Mercurial > emacs
annotate src/unexconvex.c @ 8733:14b8f8a28ace
(server-start): Always use pipes for communication.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 14 Sep 1994 09:00:44 +0000 |
parents | 1fc792473491 |
children | ac7375e60931 |
rev | line source |
---|---|
484 | 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 | |
12 This file is part of GNU Emacs. | |
13 | |
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 1, or (at your option) | |
17 any later version. | |
18 | |
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. | |
23 | |
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, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
27 | |
28 | |
29 /* modified for C-1 arch by jthomp@convex 871103 */ | |
30 /* Corrected to support convex SOFF object file formats and thread specific | |
31 * regions. streepy@convex 890302 | |
32 */ | |
33 | |
34 /* | |
35 * unexec.c - Convert a running program into an a.out file. | |
36 * | |
37 * Author: Spencer W. Thomas | |
38 * Computer Science Dept. | |
39 * University of Utah | |
40 * Date: Tue Mar 2 1982 | |
41 * Modified heavily since then. | |
42 * | |
43 * Synopsis: | |
44 * unexec (new_name, a_name, data_start, bss_start, entry_address) | |
45 * char *new_name, *a_name; | |
46 * unsigned data_start, bss_start, entry_address; | |
47 * | |
48 * Takes a snapshot of the program and makes an a.out format file in the | |
49 * file named by the string argument new_name. | |
50 * If a_name is non-NULL, the symbol table will be taken from the given file. | |
51 * On some machines, an existing a_name file is required. | |
52 * | |
53 * The boundaries within the a.out file may be adjusted with the data_start | |
54 * and bss_start arguments. Either or both may be given as 0 for defaults. | |
55 * | |
56 * Data_start gives the boundary between the text segment and the data | |
57 * segment of the program. The text segment can contain shared, read-only | |
58 * program code and literal data, while the data segment is always unshared | |
59 * and unprotected. Data_start gives the lowest unprotected address. | |
60 * The value you specify may be rounded down to a suitable boundary | |
61 * as required by the machine you are using. | |
62 * | |
63 * Specifying zero for data_start means the boundary between text and data | |
64 * should not be the same as when the program was loaded. | |
65 * If NO_REMAP is defined, the argument data_start is ignored and the | |
66 * segment boundaries are never changed. | |
67 * | |
68 * Bss_start indicates how much of the data segment is to be saved in the | |
69 * a.out file and restored when the program is executed. It gives the lowest | |
70 * unsaved address, and is rounded up to a page boundary. The default when 0 | |
71 * is given assumes that the entire data segment is to be stored, including | |
72 * the previous data and bss as well as any additional storage allocated with | |
73 * break (2). | |
74 * | |
75 * The new file is set up to start at entry_address. | |
76 * | |
77 * If you make improvements I'd like to get them too. | |
78 * harpo!utah-cs!thomas, thomas@Utah-20 | |
79 * | |
80 */ | |
81 | |
82 /* There are several compilation parameters affecting unexec: | |
83 | |
84 * COFF | |
85 | |
86 Define this if your system uses COFF for executables. | |
87 Otherwise we assume you use Berkeley format. | |
88 | |
89 * NO_REMAP | |
90 | |
91 Define this if you do not want to try to save Emacs's pure data areas | |
92 as part of the text segment. | |
93 | |
94 Saving them as text is good because it allows users to share more. | |
95 | |
96 However, on machines that locate the text area far from the data area, | |
97 the boundary cannot feasibly be moved. Such machines require | |
98 NO_REMAP. | |
99 | |
100 Also, remapping can cause trouble with the built-in startup routine | |
101 /lib/crt0.o, which defines `environ' as an initialized variable. | |
102 Dumping `environ' as pure does not work! So, to use remapping, | |
103 you must write a startup routine for your machine in Emacs's crt0.c. | |
104 If NO_REMAP is defined, Emacs uses the system's crt0.o. | |
105 | |
106 * SECTION_ALIGNMENT | |
107 | |
108 Some machines that use COFF executables require that each section | |
109 start on a certain boundary *in the COFF file*. Such machines should | |
110 define SECTION_ALIGNMENT to a mask of the low-order bits that must be | |
111 zero on such a boundary. This mask is used to control padding between | |
112 segments in the COFF file. | |
113 | |
114 If SECTION_ALIGNMENT is not defined, the segments are written | |
115 consecutively with no attempt at alignment. This is right for | |
116 unmodified system V. | |
117 | |
118 * SEGMENT_MASK | |
119 | |
120 Some machines require that the beginnings and ends of segments | |
121 *in core* be on certain boundaries. For most machines, a page | |
122 boundary is sufficient. That is the default. When a larger | |
123 boundary is needed, define SEGMENT_MASK to a mask of | |
124 the bits that must be zero on such a boundary. | |
125 | |
126 * A_TEXT_OFFSET(HDR) | |
127 | |
128 Some machines count the a.out header as part of the size of the text | |
129 segment (a_text); they may actually load the header into core as the | |
130 first data in the text segment. Some have additional padding between | |
131 the header and the real text of the program that is counted in a_text. | |
132 | |
133 For these machines, define A_TEXT_OFFSET(HDR) to examine the header | |
134 structure HDR and return the number of bytes to add to `a_text' | |
135 before writing it (above and beyond the number of bytes of actual | |
136 program text). HDR's standard fields are already correct, except that | |
137 this adjustment to the `a_text' field has not yet been made; | |
138 thus, the amount of offset can depend on the data in the file. | |
139 | |
140 * A_TEXT_SEEK(HDR) | |
141 | |
142 If defined, this macro specifies the number of bytes to seek into the | |
143 a.out file before starting to write the text segment.a | |
144 | |
145 * EXEC_MAGIC | |
146 | |
147 For machines using COFF, this macro, if defined, is a value stored | |
148 into the magic number field of the output file. | |
149 | |
150 * ADJUST_EXEC_HEADER | |
151 | |
152 This macro can be used to generate statements to adjust or | |
153 initialize nonstandard fields in the file header | |
154 | |
155 * ADDR_CORRECT(ADDR) | |
156 | |
157 Macro to correct an int which is the bit pattern of a pointer to a byte | |
158 into an int which is the number of a byte. | |
159 | |
160 This macro has a default definition which is usually right. | |
161 This default definition is a no-op on most machines (where a | |
162 pointer looks like an int) but not on all machines. | |
163 | |
164 */ | |
165 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
3591
diff
changeset
|
166 #include <config.h> |
484 | 167 #define PERROR(file) report_error (file, new) |
168 | |
169 #include <a.out.h> | |
170 /* Define getpagesize () if the system does not. | |
171 Note that this may depend on symbols defined in a.out.h | |
172 */ | |
173 #include "getpagesize.h" | |
174 | |
175 #include <sys/types.h> | |
176 #include <stdio.h> | |
177 #include <sys/stat.h> | |
178 #include <errno.h> | |
179 | |
180 extern char *start_of_text (); /* Start of text */ | |
181 extern char *start_of_data (); /* Start of initialized data */ | |
182 | |
183 #include <machine/filehdr.h> | |
184 #include <machine/opthdr.h> | |
185 #include <machine/scnhdr.h> | |
186 #include <machine/pte.h> | |
187 | |
188 static long block_copy_start; /* Old executable start point */ | |
189 static struct filehdr f_hdr; /* File header */ | |
190 static struct opthdr f_ohdr; /* Optional file header (a.out) */ | |
191 long bias; /* Bias to add for growth */ | |
192 #define SYMS_START block_copy_start | |
193 | |
194 static long text_scnptr; | |
195 static long data_scnptr; | |
196 | |
197 static int pagemask; | |
198 static int pagesz; | |
199 | |
200 static | |
201 report_error (file, fd) | |
202 char *file; | |
203 int fd; | |
204 { | |
205 if (fd) | |
206 close (fd); | |
207 error ("Failure operating on %s", file); | |
208 } | |
209 | |
210 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1 | |
211 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1 | |
212 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1 | |
213 | |
214 static | |
215 report_error_1 (fd, msg, a1, a2) | |
216 int fd; | |
217 char *msg; | |
218 int a1, a2; | |
219 { | |
220 close (fd); | |
221 error (msg, a1, a2); | |
222 } | |
223 | |
224 /* **************************************************************** | |
225 * unexec | |
226 * | |
227 * driving logic. | |
228 */ | |
229 unexec (new_name, a_name, data_start, bss_start, entry_address) | |
230 char *new_name, *a_name; | |
231 unsigned data_start, bss_start, entry_address; | |
232 { | |
233 int new, a_out = -1; | |
234 | |
235 if (a_name && (a_out = open (a_name, 0)) < 0) { | |
236 PERROR (a_name); | |
237 } | |
238 if ((new = creat (new_name, 0666)) < 0) { | |
239 PERROR (new_name); | |
240 } | |
241 | |
242 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0 | |
243 || copy_text_and_data (new) < 0 | |
244 || copy_sym (new, a_out, a_name, new_name) < 0 ) { | |
245 close (new); | |
246 return -1; | |
247 } | |
248 | |
249 close (new); | |
250 if (a_out >= 0) | |
251 close (a_out); | |
252 mark_x (new_name); | |
253 return 0; | |
254 } | |
255 | |
256 /* **************************************************************** | |
257 * make_hdr | |
258 * | |
259 * Make the header in the new a.out from the header in core. | |
260 * Modify the text and data sizes. | |
261 */ | |
262 | |
263 struct scnhdr *stbl; /* Table of all scnhdr's */ | |
264 struct scnhdr *f_thdr; /* Text section header */ | |
265 struct scnhdr *f_dhdr; /* Data section header */ | |
266 struct scnhdr *f_tdhdr; /* Thread Data section header */ | |
267 struct scnhdr *f_bhdr; /* Bss section header */ | |
268 struct scnhdr *f_tbhdr; /* Thread Bss section header */ | |
269 | |
270 static int | |
271 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) | |
272 int new, a_out; | |
273 unsigned data_start, bss_start, entry_address; | |
274 char *a_name; | |
275 char *new_name; | |
276 { | |
277 register int scns; | |
278 unsigned int bss_end; | |
279 unsigned int eo_data; /* End of initialized data in new exec file */ | |
280 int scntype; /* Section type */ | |
281 int i; /* Var for sorting by vaddr */ | |
282 struct scnhdr scntemp; /* For swapping entries in sort */ | |
283 extern char *start_of_data(); | |
284 | |
285 pagemask = (pagesz = getpagesize()) - 1; | |
286 | |
287 /* Adjust text/data boundary. */ | |
288 if (!data_start) | |
289 data_start = (unsigned) start_of_data (); | |
290 | |
291 data_start = data_start & ~pagemask; /* (Down) to page boundary. */ | |
292 | |
293 bss_end = (sbrk(0) + pagemask) & ~pagemask; | |
294 | |
295 /* Adjust data/bss boundary. */ | |
296 if (bss_start != 0) { | |
297 bss_start = (bss_start + pagemask) & ~pagemask;/* (Up) to page bdry. */ | |
298 if (bss_start > bss_end) { | |
299 ERROR1 ("unexec: Specified bss_start (%x) is past end of program", | |
300 bss_start); | |
301 } | |
302 } else | |
303 bss_start = bss_end; | |
304 | |
305 if (data_start > bss_start) { /* Can't have negative data size. */ | |
306 ERROR2 ("unexec: data_start (%x) can't be greater than bss_start (%x)", | |
307 data_start, bss_start); | |
308 } | |
309 | |
310 /* Salvage as much info from the existing file as possible */ | |
311 if (a_out < 0) { | |
312 ERROR0 ("can't build a COFF file from scratch yet"); | |
313 /*NOTREACHED*/ | |
314 } | |
315 | |
316 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) { | |
317 PERROR (a_name); | |
318 } | |
319 block_copy_start += sizeof (f_hdr); | |
320 if (f_hdr.h_opthdr > 0) { | |
321 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) { | |
322 PERROR (a_name); | |
323 } | |
324 block_copy_start += sizeof (f_ohdr); | |
325 } | |
326 | |
327 /* Allocate room for scn headers */ | |
328 stbl = (struct scnhdr *)malloc( sizeof(struct scnhdr) * f_hdr.h_nscns ); | |
329 if( stbl == NULL ) { | |
330 ERROR0( "unexec: malloc of stbl failed" ); | |
331 } | |
332 | |
333 f_tdhdr = f_tbhdr = NULL; | |
334 | |
335 /* Loop through section headers, copying them in */ | |
336 for (scns = 0; scns < f_hdr.h_nscns; scns++) { | |
337 | |
338 if( read( a_out, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) { | |
339 PERROR (a_name); | |
340 } | |
341 | |
342 scntype = stbl[scns].s_flags & S_TYPMASK; /* What type of section */ | |
343 | |
344 if( stbl[scns].s_scnptr > 0L) { | |
345 if( block_copy_start < stbl[scns].s_scnptr + stbl[scns].s_size ) | |
346 block_copy_start = stbl[scns].s_scnptr + stbl[scns].s_size; | |
347 } | |
348 | |
349 if( scntype == S_TEXT) { | |
350 f_thdr = &stbl[scns]; | |
351 } else if( scntype == S_DATA) { | |
352 f_dhdr = &stbl[scns]; | |
353 #ifdef S_TDATA | |
354 } else if( scntype == S_TDATA ) { | |
355 f_tdhdr = &stbl[scns]; | |
356 } else if( scntype == S_TBSS ) { | |
357 f_tbhdr = &stbl[scns]; | |
358 #endif /* S_TDATA (thread stuff) */ | |
359 | |
360 } else if( scntype == S_BSS) { | |
361 f_bhdr = &stbl[scns]; | |
362 } | |
363 | |
364 } | |
365 | |
366 /* We will now convert TEXT and DATA into TEXT, BSS into DATA, and leave | |
367 * all thread stuff alone. | |
368 */ | |
369 | |
370 /* Now we alter the contents of all the f_*hdr variables | |
371 to correspond to what we want to dump. */ | |
372 | |
373 f_thdr->s_vaddr = (long) start_of_text (); | |
374 f_thdr->s_size = data_start - f_thdr->s_vaddr; | |
375 f_thdr->s_scnptr = pagesz; | |
376 f_thdr->s_relptr = 0; | |
377 f_thdr->s_nrel = 0; | |
378 | |
379 eo_data = f_thdr->s_scnptr + f_thdr->s_size; | |
380 | |
381 if( f_tdhdr ) { /* Process thread data */ | |
382 | |
383 f_tdhdr->s_vaddr = data_start; | |
384 f_tdhdr->s_size += f_dhdr->s_size - (data_start - f_dhdr->s_vaddr); | |
385 f_tdhdr->s_scnptr = eo_data; | |
386 f_tdhdr->s_relptr = 0; | |
387 f_tdhdr->s_nrel = 0; | |
388 | |
389 eo_data += f_tdhdr->s_size; | |
390 | |
391 /* And now for DATA */ | |
392 | |
393 f_dhdr->s_vaddr = f_bhdr->s_vaddr; /* Take BSS start address */ | |
394 f_dhdr->s_size = bss_end - f_bhdr->s_vaddr; | |
395 f_dhdr->s_scnptr = eo_data; | |
396 f_dhdr->s_relptr = 0; | |
397 f_dhdr->s_nrel = 0; | |
398 | |
399 eo_data += f_dhdr->s_size; | |
400 | |
401 } else { | |
402 | |
403 f_dhdr->s_vaddr = data_start; | |
404 f_dhdr->s_size = bss_start - data_start; | |
405 f_dhdr->s_scnptr = eo_data; | |
406 f_dhdr->s_relptr = 0; | |
407 f_dhdr->s_nrel = 0; | |
408 | |
409 eo_data += f_dhdr->s_size; | |
410 | |
411 } | |
412 | |
413 f_bhdr->s_vaddr = bss_start; | |
414 f_bhdr->s_size = bss_end - bss_start + pagesz /* fudge */; | |
415 f_bhdr->s_scnptr = 0; | |
416 f_bhdr->s_relptr = 0; | |
417 f_bhdr->s_nrel = 0; | |
418 | |
419 text_scnptr = f_thdr->s_scnptr; | |
420 data_scnptr = f_dhdr->s_scnptr; | |
421 bias = eo_data - block_copy_start; | |
422 | |
423 if (f_ohdr.o_symptr > 0L) { | |
424 f_ohdr.o_symptr += bias; | |
425 } | |
426 | |
427 if (f_hdr.h_strptr > 0) { | |
428 f_hdr.h_strptr += bias; | |
429 } | |
430 | |
431 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) { | |
432 PERROR (new_name); | |
433 } | |
434 | |
435 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) { | |
436 PERROR (new_name); | |
437 } | |
438 | |
439 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) { | |
440 | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
484
diff
changeset
|
441 /* This is a cheesy little loop to write out the section headers |
484 | 442 * in order of increasing virtual address. Dull but effective. |
443 */ | |
444 | |
445 for( i = scns+1; i < f_hdr.h_nscns; i++ ) { | |
446 if( stbl[i].s_vaddr < stbl[scns].s_vaddr ) { /* Swap */ | |
447 scntemp = stbl[i]; | |
448 stbl[i] = stbl[scns]; | |
449 stbl[scns] = scntemp; | |
450 } | |
451 } | |
452 | |
453 } | |
454 | |
455 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) { | |
456 | |
457 if( write( new, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) { | |
458 PERROR (new_name); | |
459 } | |
460 | |
461 } | |
462 | |
463 return (0); | |
464 | |
465 } | |
466 | |
467 /* **************************************************************** | |
468 * copy_text_and_data | |
469 * | |
470 * Copy the text and data segments from memory to the new a.out | |
471 */ | |
472 static int | |
473 copy_text_and_data (new) | |
474 int new; | |
475 { | |
476 register int scns; | |
477 | |
478 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) | |
479 write_segment( new, &stbl[scns] ); | |
480 | |
481 return 0; | |
482 } | |
483 | |
484 write_segment( new, sptr ) | |
485 int new; | |
486 struct scnhdr *sptr; | |
487 { | |
488 register char *ptr, *end; | |
489 register int nwrite, ret; | |
490 char buf[80]; | |
491 extern int errno; | |
492 char zeros[128]; | |
493 | |
494 if( sptr->s_scnptr == 0 ) | |
495 return; /* Nothing to do */ | |
496 | |
497 if( lseek( new, (long) sptr->s_scnptr, 0 ) == -1 ) | |
498 PERROR( "unexecing" ); | |
499 | |
500 bzero (zeros, sizeof zeros); | |
501 | |
502 ptr = (char *) sptr->s_vaddr; | |
503 end = ptr + sptr->s_size; | |
504 | |
505 while( ptr < end ) { | |
506 | |
507 /* distance to next multiple of 128. */ | |
508 nwrite = (((int) ptr + 128) & -128) - (int) ptr; | |
509 /* But not beyond specified end. */ | |
510 if (nwrite > end - ptr) nwrite = end - ptr; | |
511 ret = write (new, ptr, nwrite); | |
512 /* If write gets a page fault, it means we reached | |
513 a gap between the old text segment and the old data segment. | |
514 This gap has probably been remapped into part of the text segment. | |
515 So write zeros for it. */ | |
516 if (ret == -1 && errno == EFAULT) | |
517 write (new, zeros, nwrite); | |
518 else if (nwrite != ret) { | |
519 sprintf (buf, | |
520 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d", | |
521 ptr, new, nwrite, ret, errno); | |
522 PERROR (buf); | |
523 } | |
524 ptr += nwrite; | |
525 } | |
526 } | |
527 | |
528 /* **************************************************************** | |
529 * copy_sym | |
530 * | |
531 * Copy the relocation information and symbol table from the a.out to the new | |
532 */ | |
533 static int | |
534 copy_sym (new, a_out, a_name, new_name) | |
535 int new, a_out; | |
536 char *a_name, *new_name; | |
537 { | |
538 char page[1024]; | |
539 int n; | |
540 | |
541 if (a_out < 0) | |
542 return 0; | |
543 | |
544 if (SYMS_START == 0L) | |
545 return 0; | |
546 | |
547 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */ | |
548 lseek( new, (long)f_ohdr.o_symptr, 0 ); | |
549 | |
550 while ((n = read (a_out, page, sizeof page)) > 0) { | |
551 if (write (new, page, n) != n) { | |
552 PERROR (new_name); | |
553 } | |
554 } | |
555 if (n < 0) { | |
556 PERROR (a_name); | |
557 } | |
558 return 0; | |
559 } | |
560 | |
561 /* **************************************************************** | |
562 * mark_x | |
563 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
484
diff
changeset
|
564 * After successfully building the new a.out, mark it executable |
484 | 565 */ |
566 static | |
567 mark_x (name) | |
568 char *name; | |
569 { | |
570 struct stat sbuf; | |
571 int um; | |
572 int new = 0; /* for PERROR */ | |
573 | |
574 um = umask (777); | |
575 umask (um); | |
576 if (stat (name, &sbuf) == -1) { | |
577 PERROR (name); | |
578 } | |
579 sbuf.st_mode |= 0111 & ~um; | |
580 if (chmod (name, sbuf.st_mode) == -1) | |
581 PERROR (name); | |
582 } | |
583 | |
584 /* Find the first pty letter. This is usually 'p', as in ptyp0, but | |
585 is sometimes configured down to 'm', 'n', or 'o' for some reason. */ | |
586 | |
587 first_pty_letter () | |
588 { | |
589 struct stat buf; | |
590 char pty_name[16]; | |
591 char c; | |
592 | |
593 for (c = 'o'; c >= 'a'; c--) | |
594 { | |
595 sprintf (pty_name, "/dev/pty%c0", c); | |
596 if (stat (pty_name, &buf) < 0) | |
597 return c + 1; | |
598 } | |
599 return 'a'; | |
600 } | |
601 |