Sync to HEAD
[bpt/emacs.git] / src / unexconvex.c
CommitLineData
d427b66a
JB
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.
9
10 Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
11
12This file is part of GNU Emacs.
13
14GNU Emacs is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
7c938215 16the Free Software Foundation; either version 2, or (at your option)
d427b66a
JB
17any later version.
18
19GNU Emacs is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
26the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27Boston, MA 02111-1307, USA. */
d427b66a
JB
28
29
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
33*/
34
35/*
36 * unexec.c - Convert a running program into an a.out file.
37 *
38 * Author: Spencer W. Thomas
39 * Computer Science Dept.
40 * University of Utah
41 * Date: Tue Mar 2 1982
42 * Modified heavily since then.
43 *
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;
48 *
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.
53 *
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.
56 *
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.
63 *
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.
68 *
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).
75 *
76 * The new file is set up to start at entry_address.
77 *
78 * If you make improvements I'd like to get them too.
79 * harpo!utah-cs!thomas, thomas@Utah-20
80 *
81 */
82
83/* There are several compilation parameters affecting unexec:
84
85* COFF
86
87Define this if your system uses COFF for executables.
88Otherwise we assume you use Berkeley format.
89
90* NO_REMAP
91
92Define this if you do not want to try to save Emacs's pure data areas
93as part of the text segment.
94
95Saving them as text is good because it allows users to share more.
96
97However, on machines that locate the text area far from the data area,
98the boundary cannot feasibly be moved. Such machines require
99NO_REMAP.
100
101Also, remapping can cause trouble with the built-in startup routine
102/lib/crt0.o, which defines `environ' as an initialized variable.
103Dumping `environ' as pure does not work! So, to use remapping,
104you must write a startup routine for your machine in Emacs's crt0.c.
105If NO_REMAP is defined, Emacs uses the system's crt0.o.
106
107* SECTION_ALIGNMENT
108
109Some machines that use COFF executables require that each section
110start on a certain boundary *in the COFF file*. Such machines should
111define SECTION_ALIGNMENT to a mask of the low-order bits that must be
112zero on such a boundary. This mask is used to control padding between
113segments in the COFF file.
114
115If SECTION_ALIGNMENT is not defined, the segments are written
116consecutively with no attempt at alignment. This is right for
117unmodified system V.
118
119* SEGMENT_MASK
120
121Some machines require that the beginnings and ends of segments
122*in core* be on certain boundaries. For most machines, a page
123boundary is sufficient. That is the default. When a larger
124boundary is needed, define SEGMENT_MASK to a mask of
125the bits that must be zero on such a boundary.
126
127* A_TEXT_OFFSET(HDR)
128
129Some machines count the a.out header as part of the size of the text
130segment (a_text); they may actually load the header into core as the
131first data in the text segment. Some have additional padding between
132the header and the real text of the program that is counted in a_text.
133
134For these machines, define A_TEXT_OFFSET(HDR) to examine the header
135structure HDR and return the number of bytes to add to `a_text'
136before writing it (above and beyond the number of bytes of actual
137program text). HDR's standard fields are already correct, except that
138this adjustment to the `a_text' field has not yet been made;
139thus, the amount of offset can depend on the data in the file.
177c0ea7 140
d427b66a
JB
141* A_TEXT_SEEK(HDR)
142
143If defined, this macro specifies the number of bytes to seek into the
144a.out file before starting to write the text segment.a
145
146* EXEC_MAGIC
147
148For machines using COFF, this macro, if defined, is a value stored
149into the magic number field of the output file.
150
151* ADJUST_EXEC_HEADER
152
153This macro can be used to generate statements to adjust or
154initialize nonstandard fields in the file header
155
156* ADDR_CORRECT(ADDR)
157
158Macro to correct an int which is the bit pattern of a pointer to a byte
159into an int which is the number of a byte.
160
161This macro has a default definition which is usually right.
162This default definition is a no-op on most machines (where a
163pointer looks like an int) but not on all machines.
164
165*/
166
18160b98 167#include <config.h>
d427b66a
JB
168#define PERROR(file) report_error (file, new)
169
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
173 */
174#include "getpagesize.h"
175
176#include <sys/types.h>
177#include <stdio.h>
178#include <sys/stat.h>
179#include <errno.h>
180
181extern char *start_of_text (); /* Start of text */
182extern char *start_of_data (); /* Start of initialized data */
183
184#include <machine/filehdr.h>
185#include <machine/opthdr.h>
186#include <machine/scnhdr.h>
187#include <machine/pte.h>
188
189static long block_copy_start; /* Old executable start point */
190static struct filehdr f_hdr; /* File header */
191static struct opthdr f_ohdr; /* Optional file header (a.out) */
192long bias; /* Bias to add for growth */
193#define SYMS_START block_copy_start
194
195static long text_scnptr;
196static long data_scnptr;
197
198static int pagemask;
199static int pagesz;
200
201static
202report_error (file, fd)
203 char *file;
204 int fd;
205{
206 if (fd)
207 close (fd);
208 error ("Failure operating on %s", file);
209}
210
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
214
215static
216report_error_1 (fd, msg, a1, a2)
217int fd;
218char *msg;
219int a1, a2;
220{
221 close (fd);
222 error (msg, a1, a2);
223}
224\f
225/* ****************************************************************
226 * unexec
227 *
228 * driving logic.
229 */
230unexec (new_name, a_name, data_start, bss_start, entry_address)
231char *new_name, *a_name;
232unsigned data_start, bss_start, entry_address;
233{
234 int new, a_out = -1;
235
236 if (a_name && (a_out = open (a_name, 0)) < 0) {
237 PERROR (a_name);
238 }
239 if ((new = creat (new_name, 0666)) < 0) {
240 PERROR (new_name);
241 }
242
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);
177c0ea7 247 return -1;
d427b66a
JB
248 }
249
250 close (new);
251 if (a_out >= 0)
252 close (a_out);
253 mark_x (new_name);
254 return 0;
255}
256
257/* ****************************************************************
258 * make_hdr
259 *
260 * Make the header in the new a.out from the header in core.
261 * Modify the text and data sizes.
262 */
263
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 */
270
271static int
272make_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;
277{
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();
285
286 pagemask = (pagesz = getpagesize()) - 1;
287
288 /* Adjust text/data boundary. */
289 if (!data_start)
290 data_start = (unsigned) start_of_data ();
291
292 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
293
294 bss_end = (sbrk(0) + pagemask) & ~pagemask;
295
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);
302 }
303 } else
304 bss_start = bss_end;
305
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);
309 }
310
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*/
315 }
316
317 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
318 PERROR (a_name);
319 }
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);
324 }
325 block_copy_start += sizeof (f_ohdr);
326 }
327
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" );
332 }
333
334 f_tdhdr = f_tbhdr = NULL;
335
336 /* Loop through section headers, copying them in */
337 for (scns = 0; scns < f_hdr.h_nscns; scns++) {
338
339 if( read( a_out, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
340 PERROR (a_name);
341 }
342
343 scntype = stbl[scns].s_flags & S_TYPMASK; /* What type of section */
344
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;
348 }
349
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) */
360
361 } else if( scntype == S_BSS) {
362 f_bhdr = &stbl[scns];
363 }
364
365 }
366
367 /* We will now convert TEXT and DATA into TEXT, BSS into DATA, and leave
368 * all thread stuff alone.
369 */
370
371 /* Now we alter the contents of all the f_*hdr variables
372 to correspond to what we want to dump. */
373
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;
379
380 eo_data = f_thdr->s_scnptr + f_thdr->s_size;
381
382 if( f_tdhdr ) { /* Process thread data */
383
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;
389
390 eo_data += f_tdhdr->s_size;
391
392 /* And now for DATA */
393
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;
399
400 eo_data += f_dhdr->s_size;
401
402 } else {
403
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;
409
410 eo_data += f_dhdr->s_size;
411
412 }
413
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;
419
420 text_scnptr = f_thdr->s_scnptr;
421 data_scnptr = f_dhdr->s_scnptr;
422 bias = eo_data - block_copy_start;
423
424 if (f_ohdr.o_symptr > 0L) {
425 f_ohdr.o_symptr += bias;
426 }
427
428 if (f_hdr.h_strptr > 0) {
429 f_hdr.h_strptr += bias;
430 }
431
432 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
433 PERROR (new_name);
434 }
435
436 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
437 PERROR (new_name);
438 }
439
440 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
441
eb8c3be9 442 /* This is a cheesy little loop to write out the section headers
d427b66a
JB
443 * in order of increasing virtual address. Dull but effective.
444 */
445
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;
451 }
452 }
453
454 }
455
456 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
457
458 if( write( new, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
459 PERROR (new_name);
460 }
461
462 }
463
464 return (0);
465
466}
467\f
468/* ****************************************************************
469 * copy_text_and_data
470 *
471 * Copy the text and data segments from memory to the new a.out
472 */
473static int
474copy_text_and_data (new)
475int new;
476{
477 register int scns;
478
479 for( scns = 0; scns < f_hdr.h_nscns; scns++ )
480 write_segment( new, &stbl[scns] );
481
482 return 0;
483}
484
485write_segment( new, sptr )
486int new;
487struct scnhdr *sptr;
488{
489 register char *ptr, *end;
490 register int nwrite, ret;
491 char buf[80];
492 extern int errno;
493 char zeros[128];
494
495 if( sptr->s_scnptr == 0 )
496 return; /* Nothing to do */
497
498 if( lseek( new, (long) sptr->s_scnptr, 0 ) == -1 )
499 PERROR( "unexecing" );
500
501 bzero (zeros, sizeof zeros);
502
503 ptr = (char *) sptr->s_vaddr;
504 end = ptr + sptr->s_size;
505
506 while( ptr < end ) {
507
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);
524 }
525 ptr += nwrite;
526 }
527}
528\f
529/* ****************************************************************
530 * copy_sym
531 *
532 * Copy the relocation information and symbol table from the a.out to the new
533 */
534static int
535copy_sym (new, a_out, a_name, new_name)
536 int new, a_out;
537 char *a_name, *new_name;
538{
539 char page[1024];
540 int n;
541
542 if (a_out < 0)
543 return 0;
544
545 if (SYMS_START == 0L)
546 return 0;
547
548 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */
549 lseek( new, (long)f_ohdr.o_symptr, 0 );
550
551 while ((n = read (a_out, page, sizeof page)) > 0) {
552 if (write (new, page, n) != n) {
553 PERROR (new_name);
554 }
555 }
556 if (n < 0) {
557 PERROR (a_name);
558 }
559 return 0;
560}
561\f
562/* ****************************************************************
563 * mark_x
564 *
eb8c3be9 565 * After successfully building the new a.out, mark it executable
d427b66a
JB
566 */
567static
568mark_x (name)
569char *name;
570{
571 struct stat sbuf;
572 int um;
573 int new = 0; /* for PERROR */
574
575 um = umask (777);
576 umask (um);
577 if (stat (name, &sbuf) == -1) {
578 PERROR (name);
579 }
580 sbuf.st_mode |= 0111 & ~um;
581 if (chmod (name, sbuf.st_mode) == -1)
582 PERROR (name);
583}
584\f
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. */
587
588first_pty_letter ()
589{
590 struct stat buf;
591 char pty_name[16];
592 char c;
593
594 for (c = 'o'; c >= 'a'; c--)
595 {
596 sprintf (pty_name, "/dev/pty%c0", c);
597 if (stat (pty_name, &buf) < 0)
598 return c + 1;
599 }
600 return 'a';
601}
602
6b61353c
KH
603/* arch-tag: 8199e06d-69b5-4f79-84d8-00f6ea929af9
604 (do not change this comment) */