Mercurial > emacs
annotate src/unexec.c @ 4775:69eba6f6d5fb
(read_minibuf): Don't add history item if it is
string-equal to the most recent history item.
author | Brian Fox <bfox@gnu.org> |
---|---|
date | Wed, 22 Sep 1993 18:13:27 +0000 |
parents | 1fc792473491 |
children | 4e5081dcfc25 |
rev | line source |
---|---|
579 | 1 /* Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc. |
172 | 2 |
3 This file is part of GNU Emacs. | |
4 | |
5 GNU Emacs is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 1, or (at your option) | |
8 any later version. | |
9 | |
10 GNU Emacs is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with GNU Emacs; see the file COPYING. If not, write to | |
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
18 | |
19 | |
20 /* | |
21 * unexec.c - Convert a running program into an a.out file. | |
22 * | |
23 * Author: Spencer W. Thomas | |
24 * Computer Science Dept. | |
25 * University of Utah | |
26 * Date: Tue Mar 2 1982 | |
27 * Modified heavily since then. | |
28 * | |
29 * Synopsis: | |
30 * unexec (new_name, a_name, data_start, bss_start, entry_address) | |
31 * char *new_name, *a_name; | |
32 * unsigned data_start, bss_start, entry_address; | |
33 * | |
34 * Takes a snapshot of the program and makes an a.out format file in the | |
35 * file named by the string argument new_name. | |
36 * If a_name is non-NULL, the symbol table will be taken from the given file. | |
37 * On some machines, an existing a_name file is required. | |
38 * | |
39 * The boundaries within the a.out file may be adjusted with the data_start | |
40 * and bss_start arguments. Either or both may be given as 0 for defaults. | |
41 * | |
42 * Data_start gives the boundary between the text segment and the data | |
43 * segment of the program. The text segment can contain shared, read-only | |
44 * program code and literal data, while the data segment is always unshared | |
45 * and unprotected. Data_start gives the lowest unprotected address. | |
46 * The value you specify may be rounded down to a suitable boundary | |
47 * as required by the machine you are using. | |
48 * | |
49 * Specifying zero for data_start means the boundary between text and data | |
50 * should not be the same as when the program was loaded. | |
51 * If NO_REMAP is defined, the argument data_start is ignored and the | |
52 * segment boundaries are never changed. | |
53 * | |
54 * Bss_start indicates how much of the data segment is to be saved in the | |
55 * a.out file and restored when the program is executed. It gives the lowest | |
56 * unsaved address, and is rounded up to a page boundary. The default when 0 | |
57 * is given assumes that the entire data segment is to be stored, including | |
58 * the previous data and bss as well as any additional storage allocated with | |
59 * break (2). | |
60 * | |
61 * The new file is set up to start at entry_address. | |
62 * | |
63 * If you make improvements I'd like to get them too. | |
64 * harpo!utah-cs!thomas, thomas@Utah-20 | |
65 * | |
66 */ | |
67 | |
68 /* Modified to support SysVr3 shared libraries by James Van Artsdalen | |
69 * of Dell Computer Corporation. james@bigtex.cactus.org. | |
70 */ | |
71 | |
72 /* There are several compilation parameters affecting unexec: | |
73 | |
74 * COFF | |
75 | |
76 Define this if your system uses COFF for executables. | |
485 | 77 |
78 * COFF_ENCAPSULATE | |
79 | |
80 Define this if you are using the GNU coff encapsulated a.out format. | |
81 This is closer to a.out than COFF. You should *not* define COFF if | |
82 you define COFF_ENCAPSULATE | |
83 | |
172 | 84 Otherwise we assume you use Berkeley format. |
85 | |
86 * NO_REMAP | |
87 | |
88 Define this if you do not want to try to save Emacs's pure data areas | |
89 as part of the text segment. | |
90 | |
91 Saving them as text is good because it allows users to share more. | |
92 | |
93 However, on machines that locate the text area far from the data area, | |
94 the boundary cannot feasibly be moved. Such machines require | |
95 NO_REMAP. | |
96 | |
97 Also, remapping can cause trouble with the built-in startup routine | |
98 /lib/crt0.o, which defines `environ' as an initialized variable. | |
99 Dumping `environ' as pure does not work! So, to use remapping, | |
100 you must write a startup routine for your machine in Emacs's crt0.c. | |
101 If NO_REMAP is defined, Emacs uses the system's crt0.o. | |
102 | |
103 * SECTION_ALIGNMENT | |
104 | |
105 Some machines that use COFF executables require that each section | |
106 start on a certain boundary *in the COFF file*. Such machines should | |
107 define SECTION_ALIGNMENT to a mask of the low-order bits that must be | |
108 zero on such a boundary. This mask is used to control padding between | |
109 segments in the COFF file. | |
110 | |
111 If SECTION_ALIGNMENT is not defined, the segments are written | |
112 consecutively with no attempt at alignment. This is right for | |
113 unmodified system V. | |
114 | |
115 * SEGMENT_MASK | |
116 | |
117 Some machines require that the beginnings and ends of segments | |
118 *in core* be on certain boundaries. For most machines, a page | |
119 boundary is sufficient. That is the default. When a larger | |
120 boundary is needed, define SEGMENT_MASK to a mask of | |
121 the bits that must be zero on such a boundary. | |
122 | |
123 * A_TEXT_OFFSET(HDR) | |
124 | |
125 Some machines count the a.out header as part of the size of the text | |
126 segment (a_text); they may actually load the header into core as the | |
127 first data in the text segment. Some have additional padding between | |
128 the header and the real text of the program that is counted in a_text. | |
129 | |
130 For these machines, define A_TEXT_OFFSET(HDR) to examine the header | |
131 structure HDR and return the number of bytes to add to `a_text' | |
132 before writing it (above and beyond the number of bytes of actual | |
133 program text). HDR's standard fields are already correct, except that | |
134 this adjustment to the `a_text' field has not yet been made; | |
135 thus, the amount of offset can depend on the data in the file. | |
136 | |
137 * A_TEXT_SEEK(HDR) | |
138 | |
139 If defined, this macro specifies the number of bytes to seek into the | |
140 a.out file before starting to write the text segment.a | |
141 | |
142 * EXEC_MAGIC | |
143 | |
144 For machines using COFF, this macro, if defined, is a value stored | |
145 into the magic number field of the output file. | |
146 | |
147 * ADJUST_EXEC_HEADER | |
148 | |
149 This macro can be used to generate statements to adjust or | |
150 initialize nonstandard fields in the file header | |
151 | |
152 * ADDR_CORRECT(ADDR) | |
153 | |
154 Macro to correct an int which is the bit pattern of a pointer to a byte | |
155 into an int which is the number of a byte. | |
156 | |
157 This macro has a default definition which is usually right. | |
158 This default definition is a no-op on most machines (where a | |
159 pointer looks like an int) but not on all machines. | |
160 | |
161 */ | |
162 | |
163 #ifndef emacs | |
164 #define PERROR(arg) perror (arg); return -1 | |
165 #else | |
166 #define IN_UNEXEC | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
4319
diff
changeset
|
167 #include <config.h> |
172 | 168 #define PERROR(file) report_error (file, new) |
169 #endif | |
170 | |
171 #ifndef CANNOT_DUMP /* all rest of file! */ | |
172 | |
173 #ifndef CANNOT_UNEXEC /* most of rest of file */ | |
174 | |
485 | 175 #ifdef COFF_ENCAPSULATE |
176 int need_coff_header = 1; | |
177 #include <coff-encap/a.out.encap.h> /* The location might be a poor assumption */ | |
178 #else | |
172 | 179 #include <a.out.h> |
485 | 180 #endif |
181 | |
172 | 182 /* Define getpagesize () if the system does not. |
183 Note that this may depend on symbols defined in a.out.h | |
184 */ | |
185 #include "getpagesize.h" | |
186 | |
187 #ifndef makedev /* Try to detect types.h already loaded */ | |
188 #include <sys/types.h> | |
485 | 189 #endif /* makedev */ |
172 | 190 #include <stdio.h> |
191 #include <sys/stat.h> | |
192 #include <errno.h> | |
193 | |
194 extern char *start_of_text (); /* Start of text */ | |
195 extern char *start_of_data (); /* Start of initialized data */ | |
196 | |
197 #ifdef COFF | |
198 static long block_copy_start; /* Old executable start point */ | |
199 static struct filehdr f_hdr; /* File header */ | |
200 static struct aouthdr f_ohdr; /* Optional file header (a.out) */ | |
201 long bias; /* Bias to add for growth */ | |
202 long lnnoptr; /* Pointer to line-number info within file */ | |
203 #define SYMS_START block_copy_start | |
204 | |
205 static long text_scnptr; | |
206 static long data_scnptr; | |
207 | |
208 #else /* not COFF */ | |
209 | |
3770
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
210 #ifdef HPUX |
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
211 extern void *sbrk (); |
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
212 #else |
620 | 213 #ifdef __STDC__ |
214 extern void *sbrk (); | |
215 #else | |
172 | 216 extern char *sbrk (); |
3770
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
217 #endif /* __STDC__ */ |
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
218 #endif /* HPUX */ |
172 | 219 |
220 #define SYMS_START ((long) N_SYMOFF (ohdr)) | |
221 | |
222 /* Some machines override the structure name for an a.out header. */ | |
223 #ifndef EXEC_HDR_TYPE | |
224 #define EXEC_HDR_TYPE struct exec | |
225 #endif | |
226 | |
227 #ifdef HPUX | |
228 #ifdef HP9000S200_ID | |
229 #define MY_ID HP9000S200_ID | |
230 #else | |
231 #include <model.h> | |
232 #define MY_ID MYSYS | |
233 #endif /* no HP9000S200_ID */ | |
234 static MAGIC OLDMAGIC = {MY_ID, SHARE_MAGIC}; | |
235 static MAGIC NEWMAGIC = {MY_ID, DEMAND_MAGIC}; | |
236 #define N_TXTOFF(x) TEXT_OFFSET(x) | |
237 #define N_SYMOFF(x) LESYM_OFFSET(x) | |
238 static EXEC_HDR_TYPE hdr, ohdr; | |
239 | |
240 #else /* not HPUX */ | |
241 | |
2917
725698689fbd
Some more changes from Michael K. Johnson for Linux.
Jim Blandy <jimb@redhat.com>
parents:
2125
diff
changeset
|
242 #if defined (USG) && !defined (IBMAIX) && !defined (IRIS) && !defined (COFF_ENCAPSULATE) && !defined (LINUX) |
172 | 243 static struct bhdr hdr, ohdr; |
244 #define a_magic fmagic | |
245 #define a_text tsize | |
246 #define a_data dsize | |
247 #define a_bss bsize | |
248 #define a_syms ssize | |
249 #define a_trsize rtsize | |
250 #define a_drsize rdsize | |
251 #define a_entry entry | |
252 #define N_BADMAG(x) \ | |
253 (((x).fmagic)!=OMAGIC && ((x).fmagic)!=NMAGIC &&\ | |
254 ((x).fmagic)!=FMAGIC && ((x).fmagic)!=IMAGIC) | |
255 #define NEWMAGIC FMAGIC | |
256 #else /* IRIS or IBMAIX or not USG */ | |
257 static EXEC_HDR_TYPE hdr, ohdr; | |
258 #define NEWMAGIC ZMAGIC | |
259 #endif /* IRIS or IBMAIX not USG */ | |
260 #endif /* not HPUX */ | |
261 | |
262 static int unexec_text_start; | |
263 static int unexec_data_start; | |
264 | |
485 | 265 #ifdef COFF_ENCAPSULATE |
266 /* coffheader is defined in the GNU a.out.encap.h file. */ | |
267 struct coffheader coffheader; | |
268 #endif | |
269 | |
172 | 270 #endif /* not COFF */ |
271 | |
272 static int pagemask; | |
273 | |
274 /* Correct an int which is the bit pattern of a pointer to a byte | |
275 into an int which is the number of a byte. | |
276 This is a no-op on ordinary machines, but not on all. */ | |
277 | |
278 #ifndef ADDR_CORRECT /* Let m-*.h files override this definition */ | |
279 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0) | |
280 #endif | |
281 | |
282 #ifdef emacs | |
283 | |
284 static | |
285 report_error (file, fd) | |
286 char *file; | |
287 int fd; | |
288 { | |
289 if (fd) | |
290 close (fd); | |
291 error ("Failure operating on %s\n", file); | |
292 } | |
293 #endif /* emacs */ | |
294 | |
295 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1 | |
296 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1 | |
297 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1 | |
298 | |
299 static | |
300 report_error_1 (fd, msg, a1, a2) | |
301 int fd; | |
302 char *msg; | |
303 int a1, a2; | |
304 { | |
305 close (fd); | |
306 #ifdef emacs | |
307 error (msg, a1, a2); | |
308 #else | |
309 fprintf (stderr, msg, a1, a2); | |
310 fprintf (stderr, "\n"); | |
311 #endif | |
312 } | |
313 | |
314 static int make_hdr (); | |
315 static int copy_text_and_data (); | |
316 static int copy_sym (); | |
317 static void mark_x (); | |
318 | |
319 /* **************************************************************** | |
320 * unexec | |
321 * | |
322 * driving logic. | |
323 */ | |
324 unexec (new_name, a_name, data_start, bss_start, entry_address) | |
325 char *new_name, *a_name; | |
326 unsigned data_start, bss_start, entry_address; | |
327 { | |
328 int new, a_out = -1; | |
329 | |
330 if (a_name && (a_out = open (a_name, 0)) < 0) | |
331 { | |
332 PERROR (a_name); | |
333 } | |
334 if ((new = creat (new_name, 0666)) < 0) | |
335 { | |
336 PERROR (new_name); | |
337 } | |
338 | |
339 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0 | |
340 || copy_text_and_data (new, a_out) < 0 | |
341 || copy_sym (new, a_out, a_name, new_name) < 0 | |
342 #ifdef COFF | |
343 #ifndef COFF_BSD_SYMBOLS | |
344 || adjust_lnnoptrs (new, a_out, new_name) < 0 | |
345 #endif | |
346 #endif | |
347 ) | |
348 { | |
349 close (new); | |
350 /* unlink (new_name); /* Failed, unlink new a.out */ | |
351 return -1; | |
352 } | |
353 | |
354 close (new); | |
355 if (a_out >= 0) | |
356 close (a_out); | |
357 mark_x (new_name); | |
358 return 0; | |
359 } | |
360 | |
361 /* **************************************************************** | |
362 * make_hdr | |
363 * | |
364 * Make the header in the new a.out from the header in core. | |
365 * Modify the text and data sizes. | |
366 */ | |
367 static int | |
368 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) | |
369 int new, a_out; | |
370 unsigned data_start, bss_start, entry_address; | |
371 char *a_name; | |
372 char *new_name; | |
373 { | |
374 int tem; | |
375 #ifdef COFF | |
376 auto struct scnhdr f_thdr; /* Text section header */ | |
377 auto struct scnhdr f_dhdr; /* Data section header */ | |
378 auto struct scnhdr f_bhdr; /* Bss section header */ | |
379 auto struct scnhdr scntemp; /* Temporary section header */ | |
380 register int scns; | |
381 #endif /* COFF */ | |
382 #ifdef USG_SHARED_LIBRARIES | |
383 extern unsigned int bss_end; | |
384 #else | |
385 unsigned int bss_end; | |
386 #endif | |
387 | |
388 pagemask = getpagesize () - 1; | |
389 | |
390 /* Adjust text/data boundary. */ | |
391 #ifdef NO_REMAP | |
392 data_start = (int) start_of_data (); | |
393 #else /* not NO_REMAP */ | |
394 if (!data_start) | |
395 data_start = (int) start_of_data (); | |
396 #endif /* not NO_REMAP */ | |
397 data_start = ADDR_CORRECT (data_start); | |
398 | |
399 #ifdef SEGMENT_MASK | |
400 data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */ | |
401 #else | |
402 data_start = data_start & ~pagemask; /* (Down) to page boundary. */ | |
403 #endif | |
404 | |
405 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask; | |
406 bss_end &= ~ pagemask; | |
407 | |
408 /* Adjust data/bss boundary. */ | |
409 if (bss_start != 0) | |
410 { | |
411 bss_start = (ADDR_CORRECT (bss_start) + pagemask); | |
412 /* (Up) to page bdry. */ | |
413 bss_start &= ~ pagemask; | |
414 if (bss_start > bss_end) | |
415 { | |
416 ERROR1 ("unexec: Specified bss_start (%u) is past end of program", | |
417 bss_start); | |
418 } | |
419 } | |
420 else | |
421 bss_start = bss_end; | |
422 | |
423 if (data_start > bss_start) /* Can't have negative data size. */ | |
424 { | |
425 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)", | |
426 data_start, bss_start); | |
427 } | |
428 | |
429 #ifdef COFF | |
430 /* Salvage as much info from the existing file as possible */ | |
431 if (a_out >= 0) | |
432 { | |
433 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | |
434 { | |
435 PERROR (a_name); | |
436 } | |
437 block_copy_start += sizeof (f_hdr); | |
438 if (f_hdr.f_opthdr > 0) | |
439 { | |
440 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
441 { | |
442 PERROR (a_name); | |
443 } | |
444 block_copy_start += sizeof (f_ohdr); | |
445 } | |
446 /* Loop through section headers, copying them in */ | |
447 for (scns = f_hdr.f_nscns; scns > 0; scns--) { | |
448 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
449 { | |
450 PERROR (a_name); | |
451 } | |
452 if (scntemp.s_scnptr > 0L) | |
453 { | |
454 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size) | |
455 block_copy_start = scntemp.s_scnptr + scntemp.s_size; | |
456 } | |
457 if (strcmp (scntemp.s_name, ".text") == 0) | |
458 { | |
459 f_thdr = scntemp; | |
460 } | |
461 else if (strcmp (scntemp.s_name, ".data") == 0) | |
462 { | |
463 f_dhdr = scntemp; | |
464 } | |
465 else if (strcmp (scntemp.s_name, ".bss") == 0) | |
466 { | |
467 f_bhdr = scntemp; | |
468 } | |
469 } | |
470 } | |
471 else | |
472 { | |
473 ERROR0 ("can't build a COFF file from scratch yet"); | |
474 } | |
475 | |
476 /* Now we alter the contents of all the f_*hdr variables | |
477 to correspond to what we want to dump. */ | |
478 | |
479 #ifdef USG_SHARED_LIBRARIES | |
480 | |
481 /* The amount of data we're adding to the file is distance from the | |
482 * end of the original .data space to the current end of the .data | |
483 * space. | |
484 */ | |
485 | |
1937
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
486 bias = bss_start - (f_ohdr.data_start + f_dhdr.s_size); |
172 | 487 |
488 #endif | |
489 | |
490 f_hdr.f_flags |= (F_RELFLG | F_EXEC); | |
491 #ifdef TPIX | |
492 f_hdr.f_nscns = 3; | |
493 #endif | |
494 #ifdef EXEC_MAGIC | |
495 f_ohdr.magic = EXEC_MAGIC; | |
496 #endif | |
497 #ifndef NO_REMAP | |
498 f_ohdr.text_start = (long) start_of_text (); | |
499 f_ohdr.tsize = data_start - f_ohdr.text_start; | |
500 f_ohdr.data_start = data_start; | |
501 #endif /* NO_REMAP */ | |
502 f_ohdr.dsize = bss_start - f_ohdr.data_start; | |
503 f_ohdr.bsize = bss_end - bss_start; | |
504 #ifndef KEEP_OLD_TEXT_SCNPTR | |
505 /* On some machines, the old values are right. | |
506 ??? Maybe on all machines with NO_REMAP. */ | |
507 f_thdr.s_size = f_ohdr.tsize; | |
508 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr); | |
509 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr)); | |
510 #endif /* KEEP_OLD_TEXT_SCNPTR */ | |
511 #ifdef ADJUST_TEXT_SCNHDR_SIZE | |
512 /* On some machines, `text size' includes all headers. */ | |
513 f_thdr.s_size -= f_thdr.s_scnptr; | |
514 #endif /* ADJUST_TEST_SCNHDR_SIZE */ | |
515 lnnoptr = f_thdr.s_lnnoptr; | |
516 #ifdef SECTION_ALIGNMENT | |
517 /* Some systems require special alignment | |
518 of the sections in the file itself. */ | |
519 f_thdr.s_scnptr | |
520 = (f_thdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT; | |
521 #endif /* SECTION_ALIGNMENT */ | |
522 #ifdef TPIX | |
523 f_thdr.s_scnptr = 0xd0; | |
524 #endif | |
525 text_scnptr = f_thdr.s_scnptr; | |
526 #ifdef ADJUST_TEXTBASE | |
527 text_scnptr = sizeof (f_hdr) + sizeof (f_ohdr) + (f_hdr.f_nscns) * (sizeof (f_thdr)); | |
528 #endif | |
529 #ifndef KEEP_OLD_PADDR | |
530 f_dhdr.s_paddr = f_ohdr.data_start; | |
531 #endif /* KEEP_OLD_PADDR */ | |
532 f_dhdr.s_vaddr = f_ohdr.data_start; | |
533 f_dhdr.s_size = f_ohdr.dsize; | |
534 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size; | |
535 #ifdef SECTION_ALIGNMENT | |
536 /* Some systems require special alignment | |
537 of the sections in the file itself. */ | |
538 f_dhdr.s_scnptr | |
539 = (f_dhdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT; | |
540 #endif /* SECTION_ALIGNMENT */ | |
541 #ifdef DATA_SECTION_ALIGNMENT | |
542 /* Some systems require special alignment | |
543 of the data section only. */ | |
544 f_dhdr.s_scnptr | |
545 = (f_dhdr.s_scnptr + DATA_SECTION_ALIGNMENT) & ~DATA_SECTION_ALIGNMENT; | |
546 #endif /* DATA_SECTION_ALIGNMENT */ | |
547 data_scnptr = f_dhdr.s_scnptr; | |
548 #ifndef KEEP_OLD_PADDR | |
549 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize; | |
550 #endif /* KEEP_OLD_PADDR */ | |
551 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize; | |
552 f_bhdr.s_size = f_ohdr.bsize; | |
553 f_bhdr.s_scnptr = 0L; | |
554 #ifndef USG_SHARED_LIBRARIES | |
555 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start; | |
556 #endif | |
557 | |
558 if (f_hdr.f_symptr > 0L) | |
559 { | |
560 f_hdr.f_symptr += bias; | |
561 } | |
562 | |
563 if (f_thdr.s_lnnoptr > 0L) | |
564 { | |
565 f_thdr.s_lnnoptr += bias; | |
566 } | |
567 | |
568 #ifdef ADJUST_EXEC_HEADER | |
569 ADJUST_EXEC_HEADER; | |
570 #endif /* ADJUST_EXEC_HEADER */ | |
571 | |
572 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | |
573 { | |
574 PERROR (new_name); | |
575 } | |
576 | |
577 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
578 { | |
579 PERROR (new_name); | |
580 } | |
581 | |
582 #ifndef USG_SHARED_LIBRARIES | |
583 | |
584 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr)) | |
585 { | |
586 PERROR (new_name); | |
587 } | |
588 | |
589 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr)) | |
590 { | |
591 PERROR (new_name); | |
592 } | |
593 | |
594 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr)) | |
595 { | |
596 PERROR (new_name); | |
597 } | |
598 | |
599 #else /* USG_SHARED_LIBRARIES */ | |
600 | |
601 /* The purpose of this code is to write out the new file's section | |
602 * header table. | |
603 * | |
604 * Scan through the original file's sections. If the encountered | |
605 * section is one we know (.text, .data or .bss), write out the | |
606 * correct header. If it is a section we do not know (such as | |
607 * .lib), adjust the address of where the section data is in the | |
608 * file, and write out the header. | |
609 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2917
diff
changeset
|
610 * If any section precedes .text or .data in the file, this code |
172 | 611 * will not adjust the file pointer for that section correctly. |
612 */ | |
613 | |
614 lseek (a_out, sizeof (f_hdr) + sizeof (f_ohdr), 0); | |
615 | |
616 for (scns = f_hdr.f_nscns; scns > 0; scns--) | |
617 { | |
618 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
619 PERROR (a_name); | |
620 | |
621 if (!strcmp (scntemp.s_name, f_thdr.s_name)) /* .text */ | |
622 { | |
623 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr)) | |
624 PERROR (new_name); | |
625 } | |
626 else if (!strcmp (scntemp.s_name, f_dhdr.s_name)) /* .data */ | |
627 { | |
628 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr)) | |
629 PERROR (new_name); | |
630 } | |
631 else if (!strcmp (scntemp.s_name, f_bhdr.s_name)) /* .bss */ | |
632 { | |
633 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr)) | |
634 PERROR (new_name); | |
635 } | |
636 else | |
637 { | |
638 if (scntemp.s_scnptr) | |
639 scntemp.s_scnptr += bias; | |
640 if (write (new, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
641 PERROR (new_name); | |
642 } | |
643 } | |
644 #endif /* USG_SHARED_LIBRARIES */ | |
645 | |
646 return (0); | |
647 | |
648 #else /* if not COFF */ | |
649 | |
650 /* Get symbol table info from header of a.out file if given one. */ | |
651 if (a_out >= 0) | |
652 { | |
485 | 653 #ifdef COFF_ENCAPSULATE |
654 if (read (a_out, &coffheader, sizeof coffheader) != sizeof coffheader) | |
655 { | |
656 PERROR(a_name); | |
657 } | |
658 if (coffheader.f_magic != COFF_MAGIC) | |
659 { | |
660 ERROR1("%s doesn't have legal coff magic number\n", a_name); | |
661 } | |
662 #endif | |
172 | 663 if (read (a_out, &ohdr, sizeof hdr) != sizeof hdr) |
664 { | |
665 PERROR (a_name); | |
666 } | |
667 | |
668 if (N_BADMAG (ohdr)) | |
669 { | |
670 ERROR1 ("invalid magic number in %s", a_name); | |
671 } | |
672 hdr = ohdr; | |
673 } | |
674 else | |
675 { | |
485 | 676 #ifdef COFF_ENCAPSULATE |
677 /* We probably could without too much trouble. The code is in gld | |
678 * but I don't have that much time or incentive. | |
679 */ | |
680 ERROR0 ("can't build a COFF file from scratch yet"); | |
681 #else | |
4319
43a327b94579
(make_hdr): Use & in call to bzero.
Richard M. Stallman <rms@gnu.org>
parents:
3770
diff
changeset
|
682 bzero (&hdr, sizeof hdr); |
485 | 683 #endif |
172 | 684 } |
685 | |
686 unexec_text_start = (long) start_of_text (); | |
687 unexec_data_start = data_start; | |
688 | |
689 /* Machine-dependent fixup for header, or maybe for unexec_text_start */ | |
690 #ifdef ADJUST_EXEC_HEADER | |
691 ADJUST_EXEC_HEADER; | |
692 #endif /* ADJUST_EXEC_HEADER */ | |
693 | |
694 hdr.a_trsize = 0; | |
695 hdr.a_drsize = 0; | |
696 if (entry_address != 0) | |
697 hdr.a_entry = entry_address; | |
698 | |
699 hdr.a_bss = bss_end - bss_start; | |
700 hdr.a_data = bss_start - data_start; | |
701 #ifdef NO_REMAP | |
702 hdr.a_text = ohdr.a_text; | |
703 #else /* not NO_REMAP */ | |
704 hdr.a_text = data_start - unexec_text_start; | |
705 | |
706 #ifdef A_TEXT_OFFSET | |
707 hdr.a_text += A_TEXT_OFFSET (ohdr); | |
708 #endif | |
709 | |
710 #endif /* not NO_REMAP */ | |
711 | |
485 | 712 #ifdef COFF_ENCAPSULATE |
713 /* We are encapsulating BSD format within COFF format. */ | |
714 { | |
715 struct coffscn *tp, *dp, *bp; | |
716 tp = &coffheader.scns[0]; | |
717 dp = &coffheader.scns[1]; | |
718 bp = &coffheader.scns[2]; | |
719 tp->s_size = hdr.a_text + sizeof(struct exec); | |
720 dp->s_paddr = data_start; | |
721 dp->s_vaddr = data_start; | |
722 dp->s_size = hdr.a_data; | |
723 bp->s_paddr = dp->s_vaddr + dp->s_size; | |
724 bp->s_vaddr = bp->s_paddr; | |
725 bp->s_size = hdr.a_bss; | |
726 coffheader.tsize = tp->s_size; | |
727 coffheader.dsize = dp->s_size; | |
728 coffheader.bsize = bp->s_size; | |
729 coffheader.text_start = tp->s_vaddr; | |
730 coffheader.data_start = dp->s_vaddr; | |
731 } | |
732 if (write (new, &coffheader, sizeof coffheader) != sizeof coffheader) | |
733 { | |
734 PERROR(new_name); | |
735 } | |
736 #endif /* COFF_ENCAPSULATE */ | |
737 | |
172 | 738 if (write (new, &hdr, sizeof hdr) != sizeof hdr) |
739 { | |
740 PERROR (new_name); | |
741 } | |
742 | |
743 #ifdef A_TEXT_OFFSET | |
744 hdr.a_text -= A_TEXT_OFFSET (ohdr); | |
745 #endif | |
746 | |
747 return 0; | |
748 | |
749 #endif /* not COFF */ | |
750 } | |
751 | |
752 /* **************************************************************** | |
753 * copy_text_and_data | |
754 * | |
755 * Copy the text and data segments from memory to the new a.out | |
756 */ | |
757 static int | |
758 copy_text_and_data (new, a_out) | |
759 int new, a_out; | |
760 { | |
761 register char *end; | |
762 register char *ptr; | |
763 | |
764 #ifdef COFF | |
765 | |
766 #ifdef USG_SHARED_LIBRARIES | |
767 | |
768 int scns; | |
769 struct scnhdr scntemp; /* Temporary section header */ | |
770 | |
771 /* The purpose of this code is to write out the new file's section | |
772 * contents. | |
773 * | |
774 * Step through the section table. If we know the section (.text, | |
775 * .data) do the appropriate thing. Otherwise, if the section has | |
776 * no allocated space in the file (.bss), do nothing. Otherwise, | |
777 * the section has space allocated in the file, and is not a section | |
778 * we know. So just copy it. | |
779 */ | |
780 | |
781 lseek (a_out, sizeof (struct filehdr) + sizeof (struct aouthdr), 0); | |
782 | |
783 for (scns = f_hdr.f_nscns; scns > 0; scns--) | |
784 { | |
785 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
786 PERROR ("temacs"); | |
787 | |
788 if (!strcmp (scntemp.s_name, ".text")) | |
789 { | |
790 lseek (new, (long) text_scnptr, 0); | |
791 ptr = (char *) f_ohdr.text_start; | |
792 end = ptr + f_ohdr.tsize; | |
793 write_segment (new, ptr, end); | |
794 } | |
795 else if (!strcmp (scntemp.s_name, ".data")) | |
796 { | |
797 lseek (new, (long) data_scnptr, 0); | |
798 ptr = (char *) f_ohdr.data_start; | |
799 end = ptr + f_ohdr.dsize; | |
800 write_segment (new, ptr, end); | |
801 } | |
802 else if (!scntemp.s_scnptr) | |
803 ; /* do nothing - no data for this section */ | |
804 else | |
805 { | |
806 char page[BUFSIZ]; | |
807 int size, n; | |
808 long old_a_out_ptr = lseek (a_out, 0, 1); | |
809 | |
810 lseek (a_out, scntemp.s_scnptr, 0); | |
811 for (size = scntemp.s_size; size > 0; size -= sizeof (page)) | |
812 { | |
813 n = size > sizeof (page) ? sizeof (page) : size; | |
814 if (read (a_out, page, n) != n || write (new, page, n) != n) | |
2125
0920d8d995d0
* unexec.c (copy_text_and_data): Error message tweaked.
Jim Blandy <jimb@redhat.com>
parents:
1937
diff
changeset
|
815 PERROR ("emacs"); |
172 | 816 } |
817 lseek (a_out, old_a_out_ptr, 0); | |
818 } | |
819 } | |
820 | |
821 #else /* COFF, but not USG_SHARED_LIBRARIES */ | |
822 | |
823 lseek (new, (long) text_scnptr, 0); | |
824 ptr = (char *) f_ohdr.text_start; | |
825 #ifdef HEADER_INCL_IN_TEXT | |
826 /* For Gould UTX/32, text starts after headers */ | |
827 ptr = (char *) (ptr + text_scnptr); | |
828 #endif /* HEADER_INCL_IN_TEXT */ | |
829 end = ptr + f_ohdr.tsize; | |
830 write_segment (new, ptr, end); | |
831 | |
832 lseek (new, (long) data_scnptr, 0); | |
833 ptr = (char *) f_ohdr.data_start; | |
834 end = ptr + f_ohdr.dsize; | |
835 write_segment (new, ptr, end); | |
836 | |
837 #endif /* USG_SHARED_LIBRARIES */ | |
838 | |
839 #else /* if not COFF */ | |
840 | |
841 /* Some machines count the header as part of the text segment. | |
842 That is to say, the header appears in core | |
843 just before the address that start_of_text () returns. | |
844 For them, N_TXTOFF is the place where the header goes. | |
845 We must adjust the seek to the place after the header. | |
846 Note that at this point hdr.a_text does *not* count | |
847 the extra A_TEXT_OFFSET bytes, only the actual bytes of code. */ | |
848 | |
849 #ifdef A_TEXT_SEEK | |
850 lseek (new, (long) A_TEXT_SEEK (hdr), 0); | |
851 #else | |
852 lseek (new, (long) N_TXTOFF (hdr), 0); | |
853 #endif /* no A_TEXT_SEEK */ | |
854 | |
855 ptr = (char *) unexec_text_start; | |
856 end = ptr + hdr.a_text; | |
857 write_segment (new, ptr, end); | |
858 | |
859 ptr = (char *) unexec_data_start; | |
860 end = ptr + hdr.a_data; | |
861 /* This lseek is certainly incorrect when A_TEXT_OFFSET | |
862 and I believe it is a no-op otherwise. | |
863 Let's see if its absence ever fails. */ | |
864 /* lseek (new, (long) N_TXTOFF (hdr) + hdr.a_text, 0); */ | |
865 write_segment (new, ptr, end); | |
866 | |
867 #endif /* not COFF */ | |
868 | |
869 return 0; | |
870 } | |
871 | |
872 write_segment (new, ptr, end) | |
873 int new; | |
874 register char *ptr, *end; | |
875 { | |
876 register int i, nwrite, ret; | |
877 char buf[80]; | |
878 extern int errno; | |
879 char zeros[128]; | |
880 | |
881 bzero (zeros, sizeof zeros); | |
882 | |
883 for (i = 0; ptr < end;) | |
884 { | |
885 /* distance to next multiple of 128. */ | |
886 nwrite = (((int) ptr + 128) & -128) - (int) ptr; | |
887 /* But not beyond specified end. */ | |
888 if (nwrite > end - ptr) nwrite = end - ptr; | |
889 ret = write (new, ptr, nwrite); | |
890 /* If write gets a page fault, it means we reached | |
891 a gap between the old text segment and the old data segment. | |
892 This gap has probably been remapped into part of the text segment. | |
893 So write zeros for it. */ | |
894 if (ret == -1 && errno == EFAULT) | |
895 write (new, zeros, nwrite); | |
896 else if (nwrite != ret) | |
897 { | |
898 sprintf (buf, | |
899 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d", | |
900 ptr, new, nwrite, ret, errno); | |
901 PERROR (buf); | |
902 } | |
903 i += nwrite; | |
904 ptr += nwrite; | |
905 } | |
906 } | |
907 | |
908 /* **************************************************************** | |
909 * copy_sym | |
910 * | |
911 * Copy the relocation information and symbol table from the a.out to the new | |
912 */ | |
913 static int | |
914 copy_sym (new, a_out, a_name, new_name) | |
915 int new, a_out; | |
916 char *a_name, *new_name; | |
917 { | |
918 char page[1024]; | |
919 int n; | |
920 | |
921 if (a_out < 0) | |
922 return 0; | |
923 | |
924 #ifdef COFF | |
925 if (SYMS_START == 0L) | |
926 return 0; | |
927 #endif /* COFF */ | |
928 | |
929 #ifdef COFF | |
930 if (lnnoptr) /* if there is line number info */ | |
931 lseek (a_out, lnnoptr, 0); /* start copying from there */ | |
932 else | |
933 #endif /* COFF */ | |
934 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */ | |
935 | |
936 while ((n = read (a_out, page, sizeof page)) > 0) | |
937 { | |
938 if (write (new, page, n) != n) | |
939 { | |
940 PERROR (new_name); | |
941 } | |
942 } | |
943 if (n < 0) | |
944 { | |
945 PERROR (a_name); | |
946 } | |
947 return 0; | |
948 } | |
949 | |
950 /* **************************************************************** | |
951 * mark_x | |
952 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2917
diff
changeset
|
953 * After successfully building the new a.out, mark it executable |
172 | 954 */ |
955 static void | |
956 mark_x (name) | |
957 char *name; | |
958 { | |
959 struct stat sbuf; | |
960 int um; | |
961 int new = 0; /* for PERROR */ | |
962 | |
963 um = umask (777); | |
964 umask (um); | |
965 if (stat (name, &sbuf) == -1) | |
966 { | |
967 PERROR (name); | |
968 } | |
969 sbuf.st_mode |= 0111 & ~um; | |
970 if (chmod (name, sbuf.st_mode) == -1) | |
971 PERROR (name); | |
972 } | |
973 | |
974 #ifdef COFF | |
975 #ifndef COFF_BSD_SYMBOLS | |
976 | |
977 /* | |
978 * If the COFF file contains a symbol table and a line number section, | |
979 * then any auxiliary entries that have values for x_lnnoptr must | |
980 * be adjusted by the amount that the line number section has moved | |
981 * in the file (bias computed in make_hdr). The #@$%&* designers of | |
982 * the auxiliary entry structures used the absolute file offsets for | |
983 * the line number entry rather than an offset from the start of the | |
984 * line number section! | |
985 * | |
986 * When I figure out how to scan through the symbol table and pick out | |
987 * the auxiliary entries that need adjustment, this routine will | |
988 * be fixed. As it is now, all such entries are wrong and sdb | |
989 * will complain. Fred Fish, UniSoft Systems Inc. | |
990 */ | |
991 | |
992 /* This function is probably very slow. Instead of reopening the new | |
993 file for input and output it should copy from the old to the new | |
994 using the two descriptors already open (WRITEDESC and READDESC). | |
995 Instead of reading one small structure at a time it should use | |
996 a reasonable size buffer. But I don't have time to work on such | |
997 things, so I am installing it as submitted to me. -- RMS. */ | |
998 | |
999 adjust_lnnoptrs (writedesc, readdesc, new_name) | |
1000 int writedesc; | |
1001 int readdesc; | |
1002 char *new_name; | |
1003 { | |
1004 register int nsyms; | |
1005 register int new; | |
579 | 1006 #if defined (amdahl_uts) || defined (pfa) |
172 | 1007 SYMENT symentry; |
1008 AUXENT auxentry; | |
1009 #else | |
1010 struct syment symentry; | |
1011 union auxent auxentry; | |
1012 #endif | |
1013 | |
1014 if (!lnnoptr || !f_hdr.f_symptr) | |
1015 return 0; | |
1016 | |
1017 if ((new = open (new_name, 2)) < 0) | |
1018 { | |
1019 PERROR (new_name); | |
1020 return -1; | |
1021 } | |
1022 | |
1023 lseek (new, f_hdr.f_symptr, 0); | |
1024 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++) | |
1025 { | |
1026 read (new, &symentry, SYMESZ); | |
1027 if (symentry.n_numaux) | |
1028 { | |
1029 read (new, &auxentry, AUXESZ); | |
1030 nsyms++; | |
1937
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1031 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400) |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1032 { |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1033 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias; |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1034 lseek (new, -AUXESZ, 1); |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1035 write (new, &auxentry, AUXESZ); |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1036 } |
172 | 1037 } |
1038 } | |
1039 close (new); | |
1040 } | |
1041 | |
1042 #endif /* COFF_BSD_SYMBOLS */ | |
1043 | |
1044 #endif /* COFF */ | |
1045 | |
1046 #endif /* not CANNOT_UNEXEC */ | |
1047 | |
1048 #endif /* not CANNOT_DUMP */ |