(mode-line-mode-menu): Update names to reflect latest changes.
[emacs.git] / src / unexconvex.c
blob3197e21a6efb5e63e48659dc0556920e670f72bb
1 /* Modified version of unexec for convex machines.
2 Note that the GNU project considers support for the peculiarities
3 of the Convex operating system a peripheral activity which should
4 not be allowed to divert effort from development of the GNU system.
5 Changes in this code will be installed when Convex system
6 maintainers send them in, but aside from that we don't plan to
7 think about it, or about whether other Emacs maintenance might
8 break it.
10 Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
12 This file is part of GNU Emacs.
14 GNU Emacs is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2, or (at your option)
17 any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. */
30 /* modified for C-1 arch by jthomp@convex 871103 */
31 /* Corrected to support convex SOFF object file formats and thread specific
32 * regions. streepy@convex 890302
36 * unexec.c - Convert a running program into an a.out file.
38 * Author: Spencer W. Thomas
39 * Computer Science Dept.
40 * University of Utah
41 * Date: Tue Mar 2 1982
42 * Modified heavily since then.
44 * Synopsis:
45 * unexec (new_name, a_name, data_start, bss_start, entry_address)
46 * char *new_name, *a_name;
47 * unsigned data_start, bss_start, entry_address;
49 * Takes a snapshot of the program and makes an a.out format file in the
50 * file named by the string argument new_name.
51 * If a_name is non-NULL, the symbol table will be taken from the given file.
52 * On some machines, an existing a_name file is required.
54 * The boundaries within the a.out file may be adjusted with the data_start
55 * and bss_start arguments. Either or both may be given as 0 for defaults.
57 * Data_start gives the boundary between the text segment and the data
58 * segment of the program. The text segment can contain shared, read-only
59 * program code and literal data, while the data segment is always unshared
60 * and unprotected. Data_start gives the lowest unprotected address.
61 * The value you specify may be rounded down to a suitable boundary
62 * as required by the machine you are using.
64 * Specifying zero for data_start means the boundary between text and data
65 * should not be the same as when the program was loaded.
66 * If NO_REMAP is defined, the argument data_start is ignored and the
67 * segment boundaries are never changed.
69 * Bss_start indicates how much of the data segment is to be saved in the
70 * a.out file and restored when the program is executed. It gives the lowest
71 * unsaved address, and is rounded up to a page boundary. The default when 0
72 * is given assumes that the entire data segment is to be stored, including
73 * the previous data and bss as well as any additional storage allocated with
74 * break (2).
76 * The new file is set up to start at entry_address.
78 * If you make improvements I'd like to get them too.
79 * harpo!utah-cs!thomas, thomas@Utah-20
83 /* There are several compilation parameters affecting unexec:
85 * COFF
87 Define this if your system uses COFF for executables.
88 Otherwise we assume you use Berkeley format.
90 * NO_REMAP
92 Define this if you do not want to try to save Emacs's pure data areas
93 as part of the text segment.
95 Saving them as text is good because it allows users to share more.
97 However, on machines that locate the text area far from the data area,
98 the boundary cannot feasibly be moved. Such machines require
99 NO_REMAP.
101 Also, remapping can cause trouble with the built-in startup routine
102 /lib/crt0.o, which defines `environ' as an initialized variable.
103 Dumping `environ' as pure does not work! So, to use remapping,
104 you must write a startup routine for your machine in Emacs's crt0.c.
105 If NO_REMAP is defined, Emacs uses the system's crt0.o.
107 * SECTION_ALIGNMENT
109 Some machines that use COFF executables require that each section
110 start on a certain boundary *in the COFF file*. Such machines should
111 define SECTION_ALIGNMENT to a mask of the low-order bits that must be
112 zero on such a boundary. This mask is used to control padding between
113 segments in the COFF file.
115 If SECTION_ALIGNMENT is not defined, the segments are written
116 consecutively with no attempt at alignment. This is right for
117 unmodified system V.
119 * SEGMENT_MASK
121 Some machines require that the beginnings and ends of segments
122 *in core* be on certain boundaries. For most machines, a page
123 boundary is sufficient. That is the default. When a larger
124 boundary is needed, define SEGMENT_MASK to a mask of
125 the bits that must be zero on such a boundary.
127 * A_TEXT_OFFSET(HDR)
129 Some machines count the a.out header as part of the size of the text
130 segment (a_text); they may actually load the header into core as the
131 first data in the text segment. Some have additional padding between
132 the header and the real text of the program that is counted in a_text.
134 For these machines, define A_TEXT_OFFSET(HDR) to examine the header
135 structure HDR and return the number of bytes to add to `a_text'
136 before writing it (above and beyond the number of bytes of actual
137 program text). HDR's standard fields are already correct, except that
138 this adjustment to the `a_text' field has not yet been made;
139 thus, the amount of offset can depend on the data in the file.
141 * A_TEXT_SEEK(HDR)
143 If defined, this macro specifies the number of bytes to seek into the
144 a.out file before starting to write the text segment.a
146 * EXEC_MAGIC
148 For machines using COFF, this macro, if defined, is a value stored
149 into the magic number field of the output file.
151 * ADJUST_EXEC_HEADER
153 This macro can be used to generate statements to adjust or
154 initialize nonstandard fields in the file header
156 * ADDR_CORRECT(ADDR)
158 Macro to correct an int which is the bit pattern of a pointer to a byte
159 into an int which is the number of a byte.
161 This macro has a default definition which is usually right.
162 This default definition is a no-op on most machines (where a
163 pointer looks like an int) but not on all machines.
167 #include <config.h>
168 #define PERROR(file) report_error (file, new)
170 #include <a.out.h>
171 /* Define getpagesize () if the system does not.
172 Note that this may depend on symbols defined in a.out.h
174 #include "getpagesize.h"
176 #include <sys/types.h>
177 #include <stdio.h>
178 #include <sys/stat.h>
179 #include <errno.h>
181 extern char *start_of_text (); /* Start of text */
182 extern char *start_of_data (); /* Start of initialized data */
184 #include <machine/filehdr.h>
185 #include <machine/opthdr.h>
186 #include <machine/scnhdr.h>
187 #include <machine/pte.h>
189 static long block_copy_start; /* Old executable start point */
190 static struct filehdr f_hdr; /* File header */
191 static struct opthdr f_ohdr; /* Optional file header (a.out) */
192 long bias; /* Bias to add for growth */
193 #define SYMS_START block_copy_start
195 static long text_scnptr;
196 static long data_scnptr;
198 static int pagemask;
199 static int pagesz;
201 static
202 report_error (file, fd)
203 char *file;
204 int fd;
206 if (fd)
207 close (fd);
208 error ("Failure operating on %s", file);
211 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
212 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
213 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
215 static
216 report_error_1 (fd, msg, a1, a2)
217 int fd;
218 char *msg;
219 int a1, a2;
221 close (fd);
222 error (msg, a1, a2);
225 /* ****************************************************************
226 * unexec
228 * driving logic.
230 unexec (new_name, a_name, data_start, bss_start, entry_address)
231 char *new_name, *a_name;
232 unsigned data_start, bss_start, entry_address;
234 int new, a_out = -1;
236 if (a_name && (a_out = open (a_name, 0)) < 0) {
237 PERROR (a_name);
239 if ((new = creat (new_name, 0666)) < 0) {
240 PERROR (new_name);
243 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
244 || copy_text_and_data (new) < 0
245 || copy_sym (new, a_out, a_name, new_name) < 0 ) {
246 close (new);
247 return -1;
250 close (new);
251 if (a_out >= 0)
252 close (a_out);
253 mark_x (new_name);
254 return 0;
257 /* ****************************************************************
258 * make_hdr
260 * Make the header in the new a.out from the header in core.
261 * Modify the text and data sizes.
264 struct scnhdr *stbl; /* Table of all scnhdr's */
265 struct scnhdr *f_thdr; /* Text section header */
266 struct scnhdr *f_dhdr; /* Data section header */
267 struct scnhdr *f_tdhdr; /* Thread Data section header */
268 struct scnhdr *f_bhdr; /* Bss section header */
269 struct scnhdr *f_tbhdr; /* Thread Bss section header */
271 static int
272 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
273 int new, a_out;
274 unsigned data_start, bss_start, entry_address;
275 char *a_name;
276 char *new_name;
278 register int scns;
279 unsigned int bss_end;
280 unsigned int eo_data; /* End of initialized data in new exec file */
281 int scntype; /* Section type */
282 int i; /* Var for sorting by vaddr */
283 struct scnhdr scntemp; /* For swapping entries in sort */
284 extern char *start_of_data();
286 pagemask = (pagesz = getpagesize()) - 1;
288 /* Adjust text/data boundary. */
289 if (!data_start)
290 data_start = (unsigned) start_of_data ();
292 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
294 bss_end = (sbrk(0) + pagemask) & ~pagemask;
296 /* Adjust data/bss boundary. */
297 if (bss_start != 0) {
298 bss_start = (bss_start + pagemask) & ~pagemask;/* (Up) to page bdry. */
299 if (bss_start > bss_end) {
300 ERROR1 ("unexec: Specified bss_start (%x) is past end of program",
301 bss_start);
303 } else
304 bss_start = bss_end;
306 if (data_start > bss_start) { /* Can't have negative data size. */
307 ERROR2 ("unexec: data_start (%x) can't be greater than bss_start (%x)",
308 data_start, bss_start);
311 /* Salvage as much info from the existing file as possible */
312 if (a_out < 0) {
313 ERROR0 ("can't build a COFF file from scratch yet");
314 /*NOTREACHED*/
317 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
318 PERROR (a_name);
320 block_copy_start += sizeof (f_hdr);
321 if (f_hdr.h_opthdr > 0) {
322 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
323 PERROR (a_name);
325 block_copy_start += sizeof (f_ohdr);
328 /* Allocate room for scn headers */
329 stbl = (struct scnhdr *)malloc( sizeof(struct scnhdr) * f_hdr.h_nscns );
330 if( stbl == NULL ) {
331 ERROR0( "unexec: malloc of stbl failed" );
334 f_tdhdr = f_tbhdr = NULL;
336 /* Loop through section headers, copying them in */
337 for (scns = 0; scns < f_hdr.h_nscns; scns++) {
339 if( read( a_out, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
340 PERROR (a_name);
343 scntype = stbl[scns].s_flags & S_TYPMASK; /* What type of section */
345 if( stbl[scns].s_scnptr > 0L) {
346 if( block_copy_start < stbl[scns].s_scnptr + stbl[scns].s_size )
347 block_copy_start = stbl[scns].s_scnptr + stbl[scns].s_size;
350 if( scntype == S_TEXT) {
351 f_thdr = &stbl[scns];
352 } else if( scntype == S_DATA) {
353 f_dhdr = &stbl[scns];
354 #ifdef S_TDATA
355 } else if( scntype == S_TDATA ) {
356 f_tdhdr = &stbl[scns];
357 } else if( scntype == S_TBSS ) {
358 f_tbhdr = &stbl[scns];
359 #endif /* S_TDATA (thread stuff) */
361 } else if( scntype == S_BSS) {
362 f_bhdr = &stbl[scns];
367 /* We will now convert TEXT and DATA into TEXT, BSS into DATA, and leave
368 * all thread stuff alone.
371 /* Now we alter the contents of all the f_*hdr variables
372 to correspond to what we want to dump. */
374 f_thdr->s_vaddr = (long) start_of_text ();
375 f_thdr->s_size = data_start - f_thdr->s_vaddr;
376 f_thdr->s_scnptr = pagesz;
377 f_thdr->s_relptr = 0;
378 f_thdr->s_nrel = 0;
380 eo_data = f_thdr->s_scnptr + f_thdr->s_size;
382 if( f_tdhdr ) { /* Process thread data */
384 f_tdhdr->s_vaddr = data_start;
385 f_tdhdr->s_size += f_dhdr->s_size - (data_start - f_dhdr->s_vaddr);
386 f_tdhdr->s_scnptr = eo_data;
387 f_tdhdr->s_relptr = 0;
388 f_tdhdr->s_nrel = 0;
390 eo_data += f_tdhdr->s_size;
392 /* And now for DATA */
394 f_dhdr->s_vaddr = f_bhdr->s_vaddr; /* Take BSS start address */
395 f_dhdr->s_size = bss_end - f_bhdr->s_vaddr;
396 f_dhdr->s_scnptr = eo_data;
397 f_dhdr->s_relptr = 0;
398 f_dhdr->s_nrel = 0;
400 eo_data += f_dhdr->s_size;
402 } else {
404 f_dhdr->s_vaddr = data_start;
405 f_dhdr->s_size = bss_start - data_start;
406 f_dhdr->s_scnptr = eo_data;
407 f_dhdr->s_relptr = 0;
408 f_dhdr->s_nrel = 0;
410 eo_data += f_dhdr->s_size;
414 f_bhdr->s_vaddr = bss_start;
415 f_bhdr->s_size = bss_end - bss_start + pagesz /* fudge */;
416 f_bhdr->s_scnptr = 0;
417 f_bhdr->s_relptr = 0;
418 f_bhdr->s_nrel = 0;
420 text_scnptr = f_thdr->s_scnptr;
421 data_scnptr = f_dhdr->s_scnptr;
422 bias = eo_data - block_copy_start;
424 if (f_ohdr.o_symptr > 0L) {
425 f_ohdr.o_symptr += bias;
428 if (f_hdr.h_strptr > 0) {
429 f_hdr.h_strptr += bias;
432 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
433 PERROR (new_name);
436 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
437 PERROR (new_name);
440 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
442 /* This is a cheesy little loop to write out the section headers
443 * in order of increasing virtual address. Dull but effective.
446 for( i = scns+1; i < f_hdr.h_nscns; i++ ) {
447 if( stbl[i].s_vaddr < stbl[scns].s_vaddr ) { /* Swap */
448 scntemp = stbl[i];
449 stbl[i] = stbl[scns];
450 stbl[scns] = scntemp;
456 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
458 if( write( new, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
459 PERROR (new_name);
464 return (0);
468 /* ****************************************************************
469 * copy_text_and_data
471 * Copy the text and data segments from memory to the new a.out
473 static int
474 copy_text_and_data (new)
475 int new;
477 register int scns;
479 for( scns = 0; scns < f_hdr.h_nscns; scns++ )
480 write_segment( new, &stbl[scns] );
482 return 0;
485 write_segment( new, sptr )
486 int new;
487 struct scnhdr *sptr;
489 register char *ptr, *end;
490 register int nwrite, ret;
491 char buf[80];
492 extern int errno;
493 char zeros[128];
495 if( sptr->s_scnptr == 0 )
496 return; /* Nothing to do */
498 if( lseek( new, (long) sptr->s_scnptr, 0 ) == -1 )
499 PERROR( "unexecing" );
501 bzero (zeros, sizeof zeros);
503 ptr = (char *) sptr->s_vaddr;
504 end = ptr + sptr->s_size;
506 while( ptr < end ) {
508 /* distance to next multiple of 128. */
509 nwrite = (((int) ptr + 128) & -128) - (int) ptr;
510 /* But not beyond specified end. */
511 if (nwrite > end - ptr) nwrite = end - ptr;
512 ret = write (new, ptr, nwrite);
513 /* If write gets a page fault, it means we reached
514 a gap between the old text segment and the old data segment.
515 This gap has probably been remapped into part of the text segment.
516 So write zeros for it. */
517 if (ret == -1 && errno == EFAULT)
518 write (new, zeros, nwrite);
519 else if (nwrite != ret) {
520 sprintf (buf,
521 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
522 ptr, new, nwrite, ret, errno);
523 PERROR (buf);
525 ptr += nwrite;
529 /* ****************************************************************
530 * copy_sym
532 * Copy the relocation information and symbol table from the a.out to the new
534 static int
535 copy_sym (new, a_out, a_name, new_name)
536 int new, a_out;
537 char *a_name, *new_name;
539 char page[1024];
540 int n;
542 if (a_out < 0)
543 return 0;
545 if (SYMS_START == 0L)
546 return 0;
548 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */
549 lseek( new, (long)f_ohdr.o_symptr, 0 );
551 while ((n = read (a_out, page, sizeof page)) > 0) {
552 if (write (new, page, n) != n) {
553 PERROR (new_name);
556 if (n < 0) {
557 PERROR (a_name);
559 return 0;
562 /* ****************************************************************
563 * mark_x
565 * After successfully building the new a.out, mark it executable
567 static
568 mark_x (name)
569 char *name;
571 struct stat sbuf;
572 int um;
573 int new = 0; /* for PERROR */
575 um = umask (777);
576 umask (um);
577 if (stat (name, &sbuf) == -1) {
578 PERROR (name);
580 sbuf.st_mode |= 0111 & ~um;
581 if (chmod (name, sbuf.st_mode) == -1)
582 PERROR (name);
585 /* Find the first pty letter. This is usually 'p', as in ptyp0, but
586 is sometimes configured down to 'm', 'n', or 'o' for some reason. */
588 first_pty_letter ()
590 struct stat buf;
591 char pty_name[16];
592 char c;
594 for (c = 'o'; c >= 'a'; c--)
596 sprintf (pty_name, "/dev/pty%c0", c);
597 if (stat (pty_name, &buf) < 0)
598 return c + 1;
600 return 'a';