Mercurial > emacs
annotate src/unexec.c @ 5163:220ec2f456e0
Comment change.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 25 Nov 1993 02:37:21 +0000 |
parents | 4e5081dcfc25 |
children | 6f6637309b38 |
rev | line source |
---|---|
4973
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
1 /* Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993 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 | |
4973
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
7 the Free Software Foundation; either version 2, or (at your option) |
172 | 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 |
4973
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
213 #if 0 |
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
214 /* Some systems with __STDC__ compilers still declare this `char *' in some |
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
215 header file, and our declaration conflicts. The return value is always |
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
216 cast, so it should be harmless to leave it undefined. Hopefully |
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
217 machines with different size pointers and ints declare sbrk in a header |
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
218 file. */ |
620 | 219 #ifdef __STDC__ |
220 extern void *sbrk (); | |
221 #else | |
172 | 222 extern char *sbrk (); |
3770
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
223 #endif /* __STDC__ */ |
4973
4e5081dcfc25
[! HPUX]: Don't declare sbrk at all, so as not to conflict with headers.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
224 #endif |
3770
07ba80692381
* unexec.c [HPUX] (sbrk): This returns a void *.
Jim Blandy <jimb@redhat.com>
parents:
3591
diff
changeset
|
225 #endif /* HPUX */ |
172 | 226 |
227 #define SYMS_START ((long) N_SYMOFF (ohdr)) | |
228 | |
229 /* Some machines override the structure name for an a.out header. */ | |
230 #ifndef EXEC_HDR_TYPE | |
231 #define EXEC_HDR_TYPE struct exec | |
232 #endif | |
233 | |
234 #ifdef HPUX | |
235 #ifdef HP9000S200_ID | |
236 #define MY_ID HP9000S200_ID | |
237 #else | |
238 #include <model.h> | |
239 #define MY_ID MYSYS | |
240 #endif /* no HP9000S200_ID */ | |
241 static MAGIC OLDMAGIC = {MY_ID, SHARE_MAGIC}; | |
242 static MAGIC NEWMAGIC = {MY_ID, DEMAND_MAGIC}; | |
243 #define N_TXTOFF(x) TEXT_OFFSET(x) | |
244 #define N_SYMOFF(x) LESYM_OFFSET(x) | |
245 static EXEC_HDR_TYPE hdr, ohdr; | |
246 | |
247 #else /* not HPUX */ | |
248 | |
2917
725698689fbd
Some more changes from Michael K. Johnson for Linux.
Jim Blandy <jimb@redhat.com>
parents:
2125
diff
changeset
|
249 #if defined (USG) && !defined (IBMAIX) && !defined (IRIS) && !defined (COFF_ENCAPSULATE) && !defined (LINUX) |
172 | 250 static struct bhdr hdr, ohdr; |
251 #define a_magic fmagic | |
252 #define a_text tsize | |
253 #define a_data dsize | |
254 #define a_bss bsize | |
255 #define a_syms ssize | |
256 #define a_trsize rtsize | |
257 #define a_drsize rdsize | |
258 #define a_entry entry | |
259 #define N_BADMAG(x) \ | |
260 (((x).fmagic)!=OMAGIC && ((x).fmagic)!=NMAGIC &&\ | |
261 ((x).fmagic)!=FMAGIC && ((x).fmagic)!=IMAGIC) | |
262 #define NEWMAGIC FMAGIC | |
263 #else /* IRIS or IBMAIX or not USG */ | |
264 static EXEC_HDR_TYPE hdr, ohdr; | |
265 #define NEWMAGIC ZMAGIC | |
266 #endif /* IRIS or IBMAIX not USG */ | |
267 #endif /* not HPUX */ | |
268 | |
269 static int unexec_text_start; | |
270 static int unexec_data_start; | |
271 | |
485 | 272 #ifdef COFF_ENCAPSULATE |
273 /* coffheader is defined in the GNU a.out.encap.h file. */ | |
274 struct coffheader coffheader; | |
275 #endif | |
276 | |
172 | 277 #endif /* not COFF */ |
278 | |
279 static int pagemask; | |
280 | |
281 /* Correct an int which is the bit pattern of a pointer to a byte | |
282 into an int which is the number of a byte. | |
283 This is a no-op on ordinary machines, but not on all. */ | |
284 | |
285 #ifndef ADDR_CORRECT /* Let m-*.h files override this definition */ | |
286 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0) | |
287 #endif | |
288 | |
289 #ifdef emacs | |
290 | |
291 static | |
292 report_error (file, fd) | |
293 char *file; | |
294 int fd; | |
295 { | |
296 if (fd) | |
297 close (fd); | |
298 error ("Failure operating on %s\n", file); | |
299 } | |
300 #endif /* emacs */ | |
301 | |
302 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1 | |
303 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1 | |
304 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1 | |
305 | |
306 static | |
307 report_error_1 (fd, msg, a1, a2) | |
308 int fd; | |
309 char *msg; | |
310 int a1, a2; | |
311 { | |
312 close (fd); | |
313 #ifdef emacs | |
314 error (msg, a1, a2); | |
315 #else | |
316 fprintf (stderr, msg, a1, a2); | |
317 fprintf (stderr, "\n"); | |
318 #endif | |
319 } | |
320 | |
321 static int make_hdr (); | |
322 static int copy_text_and_data (); | |
323 static int copy_sym (); | |
324 static void mark_x (); | |
325 | |
326 /* **************************************************************** | |
327 * unexec | |
328 * | |
329 * driving logic. | |
330 */ | |
331 unexec (new_name, a_name, data_start, bss_start, entry_address) | |
332 char *new_name, *a_name; | |
333 unsigned data_start, bss_start, entry_address; | |
334 { | |
335 int new, a_out = -1; | |
336 | |
337 if (a_name && (a_out = open (a_name, 0)) < 0) | |
338 { | |
339 PERROR (a_name); | |
340 } | |
341 if ((new = creat (new_name, 0666)) < 0) | |
342 { | |
343 PERROR (new_name); | |
344 } | |
345 | |
346 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0 | |
347 || copy_text_and_data (new, a_out) < 0 | |
348 || copy_sym (new, a_out, a_name, new_name) < 0 | |
349 #ifdef COFF | |
350 #ifndef COFF_BSD_SYMBOLS | |
351 || adjust_lnnoptrs (new, a_out, new_name) < 0 | |
352 #endif | |
353 #endif | |
354 ) | |
355 { | |
356 close (new); | |
357 /* unlink (new_name); /* Failed, unlink new a.out */ | |
358 return -1; | |
359 } | |
360 | |
361 close (new); | |
362 if (a_out >= 0) | |
363 close (a_out); | |
364 mark_x (new_name); | |
365 return 0; | |
366 } | |
367 | |
368 /* **************************************************************** | |
369 * make_hdr | |
370 * | |
371 * Make the header in the new a.out from the header in core. | |
372 * Modify the text and data sizes. | |
373 */ | |
374 static int | |
375 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) | |
376 int new, a_out; | |
377 unsigned data_start, bss_start, entry_address; | |
378 char *a_name; | |
379 char *new_name; | |
380 { | |
381 int tem; | |
382 #ifdef COFF | |
383 auto struct scnhdr f_thdr; /* Text section header */ | |
384 auto struct scnhdr f_dhdr; /* Data section header */ | |
385 auto struct scnhdr f_bhdr; /* Bss section header */ | |
386 auto struct scnhdr scntemp; /* Temporary section header */ | |
387 register int scns; | |
388 #endif /* COFF */ | |
389 #ifdef USG_SHARED_LIBRARIES | |
390 extern unsigned int bss_end; | |
391 #else | |
392 unsigned int bss_end; | |
393 #endif | |
394 | |
395 pagemask = getpagesize () - 1; | |
396 | |
397 /* Adjust text/data boundary. */ | |
398 #ifdef NO_REMAP | |
399 data_start = (int) start_of_data (); | |
400 #else /* not NO_REMAP */ | |
401 if (!data_start) | |
402 data_start = (int) start_of_data (); | |
403 #endif /* not NO_REMAP */ | |
404 data_start = ADDR_CORRECT (data_start); | |
405 | |
406 #ifdef SEGMENT_MASK | |
407 data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */ | |
408 #else | |
409 data_start = data_start & ~pagemask; /* (Down) to page boundary. */ | |
410 #endif | |
411 | |
412 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask; | |
413 bss_end &= ~ pagemask; | |
414 | |
415 /* Adjust data/bss boundary. */ | |
416 if (bss_start != 0) | |
417 { | |
418 bss_start = (ADDR_CORRECT (bss_start) + pagemask); | |
419 /* (Up) to page bdry. */ | |
420 bss_start &= ~ pagemask; | |
421 if (bss_start > bss_end) | |
422 { | |
423 ERROR1 ("unexec: Specified bss_start (%u) is past end of program", | |
424 bss_start); | |
425 } | |
426 } | |
427 else | |
428 bss_start = bss_end; | |
429 | |
430 if (data_start > bss_start) /* Can't have negative data size. */ | |
431 { | |
432 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)", | |
433 data_start, bss_start); | |
434 } | |
435 | |
436 #ifdef COFF | |
437 /* Salvage as much info from the existing file as possible */ | |
438 if (a_out >= 0) | |
439 { | |
440 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | |
441 { | |
442 PERROR (a_name); | |
443 } | |
444 block_copy_start += sizeof (f_hdr); | |
445 if (f_hdr.f_opthdr > 0) | |
446 { | |
447 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
448 { | |
449 PERROR (a_name); | |
450 } | |
451 block_copy_start += sizeof (f_ohdr); | |
452 } | |
453 /* Loop through section headers, copying them in */ | |
454 for (scns = f_hdr.f_nscns; scns > 0; scns--) { | |
455 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
456 { | |
457 PERROR (a_name); | |
458 } | |
459 if (scntemp.s_scnptr > 0L) | |
460 { | |
461 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size) | |
462 block_copy_start = scntemp.s_scnptr + scntemp.s_size; | |
463 } | |
464 if (strcmp (scntemp.s_name, ".text") == 0) | |
465 { | |
466 f_thdr = scntemp; | |
467 } | |
468 else if (strcmp (scntemp.s_name, ".data") == 0) | |
469 { | |
470 f_dhdr = scntemp; | |
471 } | |
472 else if (strcmp (scntemp.s_name, ".bss") == 0) | |
473 { | |
474 f_bhdr = scntemp; | |
475 } | |
476 } | |
477 } | |
478 else | |
479 { | |
480 ERROR0 ("can't build a COFF file from scratch yet"); | |
481 } | |
482 | |
483 /* Now we alter the contents of all the f_*hdr variables | |
484 to correspond to what we want to dump. */ | |
485 | |
486 #ifdef USG_SHARED_LIBRARIES | |
487 | |
488 /* The amount of data we're adding to the file is distance from the | |
489 * end of the original .data space to the current end of the .data | |
490 * space. | |
491 */ | |
492 | |
1937
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
493 bias = bss_start - (f_ohdr.data_start + f_dhdr.s_size); |
172 | 494 |
495 #endif | |
496 | |
497 f_hdr.f_flags |= (F_RELFLG | F_EXEC); | |
498 #ifdef TPIX | |
499 f_hdr.f_nscns = 3; | |
500 #endif | |
501 #ifdef EXEC_MAGIC | |
502 f_ohdr.magic = EXEC_MAGIC; | |
503 #endif | |
504 #ifndef NO_REMAP | |
505 f_ohdr.text_start = (long) start_of_text (); | |
506 f_ohdr.tsize = data_start - f_ohdr.text_start; | |
507 f_ohdr.data_start = data_start; | |
508 #endif /* NO_REMAP */ | |
509 f_ohdr.dsize = bss_start - f_ohdr.data_start; | |
510 f_ohdr.bsize = bss_end - bss_start; | |
511 #ifndef KEEP_OLD_TEXT_SCNPTR | |
512 /* On some machines, the old values are right. | |
513 ??? Maybe on all machines with NO_REMAP. */ | |
514 f_thdr.s_size = f_ohdr.tsize; | |
515 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr); | |
516 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr)); | |
517 #endif /* KEEP_OLD_TEXT_SCNPTR */ | |
518 #ifdef ADJUST_TEXT_SCNHDR_SIZE | |
519 /* On some machines, `text size' includes all headers. */ | |
520 f_thdr.s_size -= f_thdr.s_scnptr; | |
521 #endif /* ADJUST_TEST_SCNHDR_SIZE */ | |
522 lnnoptr = f_thdr.s_lnnoptr; | |
523 #ifdef SECTION_ALIGNMENT | |
524 /* Some systems require special alignment | |
525 of the sections in the file itself. */ | |
526 f_thdr.s_scnptr | |
527 = (f_thdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT; | |
528 #endif /* SECTION_ALIGNMENT */ | |
529 #ifdef TPIX | |
530 f_thdr.s_scnptr = 0xd0; | |
531 #endif | |
532 text_scnptr = f_thdr.s_scnptr; | |
533 #ifdef ADJUST_TEXTBASE | |
534 text_scnptr = sizeof (f_hdr) + sizeof (f_ohdr) + (f_hdr.f_nscns) * (sizeof (f_thdr)); | |
535 #endif | |
536 #ifndef KEEP_OLD_PADDR | |
537 f_dhdr.s_paddr = f_ohdr.data_start; | |
538 #endif /* KEEP_OLD_PADDR */ | |
539 f_dhdr.s_vaddr = f_ohdr.data_start; | |
540 f_dhdr.s_size = f_ohdr.dsize; | |
541 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size; | |
542 #ifdef SECTION_ALIGNMENT | |
543 /* Some systems require special alignment | |
544 of the sections in the file itself. */ | |
545 f_dhdr.s_scnptr | |
546 = (f_dhdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT; | |
547 #endif /* SECTION_ALIGNMENT */ | |
548 #ifdef DATA_SECTION_ALIGNMENT | |
549 /* Some systems require special alignment | |
550 of the data section only. */ | |
551 f_dhdr.s_scnptr | |
552 = (f_dhdr.s_scnptr + DATA_SECTION_ALIGNMENT) & ~DATA_SECTION_ALIGNMENT; | |
553 #endif /* DATA_SECTION_ALIGNMENT */ | |
554 data_scnptr = f_dhdr.s_scnptr; | |
555 #ifndef KEEP_OLD_PADDR | |
556 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize; | |
557 #endif /* KEEP_OLD_PADDR */ | |
558 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize; | |
559 f_bhdr.s_size = f_ohdr.bsize; | |
560 f_bhdr.s_scnptr = 0L; | |
561 #ifndef USG_SHARED_LIBRARIES | |
562 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start; | |
563 #endif | |
564 | |
565 if (f_hdr.f_symptr > 0L) | |
566 { | |
567 f_hdr.f_symptr += bias; | |
568 } | |
569 | |
570 if (f_thdr.s_lnnoptr > 0L) | |
571 { | |
572 f_thdr.s_lnnoptr += bias; | |
573 } | |
574 | |
575 #ifdef ADJUST_EXEC_HEADER | |
576 ADJUST_EXEC_HEADER; | |
577 #endif /* ADJUST_EXEC_HEADER */ | |
578 | |
579 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | |
580 { | |
581 PERROR (new_name); | |
582 } | |
583 | |
584 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
585 { | |
586 PERROR (new_name); | |
587 } | |
588 | |
589 #ifndef USG_SHARED_LIBRARIES | |
590 | |
591 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr)) | |
592 { | |
593 PERROR (new_name); | |
594 } | |
595 | |
596 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr)) | |
597 { | |
598 PERROR (new_name); | |
599 } | |
600 | |
601 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr)) | |
602 { | |
603 PERROR (new_name); | |
604 } | |
605 | |
606 #else /* USG_SHARED_LIBRARIES */ | |
607 | |
608 /* The purpose of this code is to write out the new file's section | |
609 * header table. | |
610 * | |
611 * Scan through the original file's sections. If the encountered | |
612 * section is one we know (.text, .data or .bss), write out the | |
613 * correct header. If it is a section we do not know (such as | |
614 * .lib), adjust the address of where the section data is in the | |
615 * file, and write out the header. | |
616 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2917
diff
changeset
|
617 * If any section precedes .text or .data in the file, this code |
172 | 618 * will not adjust the file pointer for that section correctly. |
619 */ | |
620 | |
621 lseek (a_out, sizeof (f_hdr) + sizeof (f_ohdr), 0); | |
622 | |
623 for (scns = f_hdr.f_nscns; scns > 0; scns--) | |
624 { | |
625 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
626 PERROR (a_name); | |
627 | |
628 if (!strcmp (scntemp.s_name, f_thdr.s_name)) /* .text */ | |
629 { | |
630 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr)) | |
631 PERROR (new_name); | |
632 } | |
633 else if (!strcmp (scntemp.s_name, f_dhdr.s_name)) /* .data */ | |
634 { | |
635 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr)) | |
636 PERROR (new_name); | |
637 } | |
638 else if (!strcmp (scntemp.s_name, f_bhdr.s_name)) /* .bss */ | |
639 { | |
640 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr)) | |
641 PERROR (new_name); | |
642 } | |
643 else | |
644 { | |
645 if (scntemp.s_scnptr) | |
646 scntemp.s_scnptr += bias; | |
647 if (write (new, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
648 PERROR (new_name); | |
649 } | |
650 } | |
651 #endif /* USG_SHARED_LIBRARIES */ | |
652 | |
653 return (0); | |
654 | |
655 #else /* if not COFF */ | |
656 | |
657 /* Get symbol table info from header of a.out file if given one. */ | |
658 if (a_out >= 0) | |
659 { | |
485 | 660 #ifdef COFF_ENCAPSULATE |
661 if (read (a_out, &coffheader, sizeof coffheader) != sizeof coffheader) | |
662 { | |
663 PERROR(a_name); | |
664 } | |
665 if (coffheader.f_magic != COFF_MAGIC) | |
666 { | |
667 ERROR1("%s doesn't have legal coff magic number\n", a_name); | |
668 } | |
669 #endif | |
172 | 670 if (read (a_out, &ohdr, sizeof hdr) != sizeof hdr) |
671 { | |
672 PERROR (a_name); | |
673 } | |
674 | |
675 if (N_BADMAG (ohdr)) | |
676 { | |
677 ERROR1 ("invalid magic number in %s", a_name); | |
678 } | |
679 hdr = ohdr; | |
680 } | |
681 else | |
682 { | |
485 | 683 #ifdef COFF_ENCAPSULATE |
684 /* We probably could without too much trouble. The code is in gld | |
685 * but I don't have that much time or incentive. | |
686 */ | |
687 ERROR0 ("can't build a COFF file from scratch yet"); | |
688 #else | |
4319
43a327b94579
(make_hdr): Use & in call to bzero.
Richard M. Stallman <rms@gnu.org>
parents:
3770
diff
changeset
|
689 bzero (&hdr, sizeof hdr); |
485 | 690 #endif |
172 | 691 } |
692 | |
693 unexec_text_start = (long) start_of_text (); | |
694 unexec_data_start = data_start; | |
695 | |
696 /* Machine-dependent fixup for header, or maybe for unexec_text_start */ | |
697 #ifdef ADJUST_EXEC_HEADER | |
698 ADJUST_EXEC_HEADER; | |
699 #endif /* ADJUST_EXEC_HEADER */ | |
700 | |
701 hdr.a_trsize = 0; | |
702 hdr.a_drsize = 0; | |
703 if (entry_address != 0) | |
704 hdr.a_entry = entry_address; | |
705 | |
706 hdr.a_bss = bss_end - bss_start; | |
707 hdr.a_data = bss_start - data_start; | |
708 #ifdef NO_REMAP | |
709 hdr.a_text = ohdr.a_text; | |
710 #else /* not NO_REMAP */ | |
711 hdr.a_text = data_start - unexec_text_start; | |
712 | |
713 #ifdef A_TEXT_OFFSET | |
714 hdr.a_text += A_TEXT_OFFSET (ohdr); | |
715 #endif | |
716 | |
717 #endif /* not NO_REMAP */ | |
718 | |
485 | 719 #ifdef COFF_ENCAPSULATE |
720 /* We are encapsulating BSD format within COFF format. */ | |
721 { | |
722 struct coffscn *tp, *dp, *bp; | |
723 tp = &coffheader.scns[0]; | |
724 dp = &coffheader.scns[1]; | |
725 bp = &coffheader.scns[2]; | |
726 tp->s_size = hdr.a_text + sizeof(struct exec); | |
727 dp->s_paddr = data_start; | |
728 dp->s_vaddr = data_start; | |
729 dp->s_size = hdr.a_data; | |
730 bp->s_paddr = dp->s_vaddr + dp->s_size; | |
731 bp->s_vaddr = bp->s_paddr; | |
732 bp->s_size = hdr.a_bss; | |
733 coffheader.tsize = tp->s_size; | |
734 coffheader.dsize = dp->s_size; | |
735 coffheader.bsize = bp->s_size; | |
736 coffheader.text_start = tp->s_vaddr; | |
737 coffheader.data_start = dp->s_vaddr; | |
738 } | |
739 if (write (new, &coffheader, sizeof coffheader) != sizeof coffheader) | |
740 { | |
741 PERROR(new_name); | |
742 } | |
743 #endif /* COFF_ENCAPSULATE */ | |
744 | |
172 | 745 if (write (new, &hdr, sizeof hdr) != sizeof hdr) |
746 { | |
747 PERROR (new_name); | |
748 } | |
749 | |
750 #ifdef A_TEXT_OFFSET | |
751 hdr.a_text -= A_TEXT_OFFSET (ohdr); | |
752 #endif | |
753 | |
754 return 0; | |
755 | |
756 #endif /* not COFF */ | |
757 } | |
758 | |
759 /* **************************************************************** | |
760 * copy_text_and_data | |
761 * | |
762 * Copy the text and data segments from memory to the new a.out | |
763 */ | |
764 static int | |
765 copy_text_and_data (new, a_out) | |
766 int new, a_out; | |
767 { | |
768 register char *end; | |
769 register char *ptr; | |
770 | |
771 #ifdef COFF | |
772 | |
773 #ifdef USG_SHARED_LIBRARIES | |
774 | |
775 int scns; | |
776 struct scnhdr scntemp; /* Temporary section header */ | |
777 | |
778 /* The purpose of this code is to write out the new file's section | |
779 * contents. | |
780 * | |
781 * Step through the section table. If we know the section (.text, | |
782 * .data) do the appropriate thing. Otherwise, if the section has | |
783 * no allocated space in the file (.bss), do nothing. Otherwise, | |
784 * the section has space allocated in the file, and is not a section | |
785 * we know. So just copy it. | |
786 */ | |
787 | |
788 lseek (a_out, sizeof (struct filehdr) + sizeof (struct aouthdr), 0); | |
789 | |
790 for (scns = f_hdr.f_nscns; scns > 0; scns--) | |
791 { | |
792 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
793 PERROR ("temacs"); | |
794 | |
795 if (!strcmp (scntemp.s_name, ".text")) | |
796 { | |
797 lseek (new, (long) text_scnptr, 0); | |
798 ptr = (char *) f_ohdr.text_start; | |
799 end = ptr + f_ohdr.tsize; | |
800 write_segment (new, ptr, end); | |
801 } | |
802 else if (!strcmp (scntemp.s_name, ".data")) | |
803 { | |
804 lseek (new, (long) data_scnptr, 0); | |
805 ptr = (char *) f_ohdr.data_start; | |
806 end = ptr + f_ohdr.dsize; | |
807 write_segment (new, ptr, end); | |
808 } | |
809 else if (!scntemp.s_scnptr) | |
810 ; /* do nothing - no data for this section */ | |
811 else | |
812 { | |
813 char page[BUFSIZ]; | |
814 int size, n; | |
815 long old_a_out_ptr = lseek (a_out, 0, 1); | |
816 | |
817 lseek (a_out, scntemp.s_scnptr, 0); | |
818 for (size = scntemp.s_size; size > 0; size -= sizeof (page)) | |
819 { | |
820 n = size > sizeof (page) ? sizeof (page) : size; | |
821 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
|
822 PERROR ("emacs"); |
172 | 823 } |
824 lseek (a_out, old_a_out_ptr, 0); | |
825 } | |
826 } | |
827 | |
828 #else /* COFF, but not USG_SHARED_LIBRARIES */ | |
829 | |
830 lseek (new, (long) text_scnptr, 0); | |
831 ptr = (char *) f_ohdr.text_start; | |
832 #ifdef HEADER_INCL_IN_TEXT | |
833 /* For Gould UTX/32, text starts after headers */ | |
834 ptr = (char *) (ptr + text_scnptr); | |
835 #endif /* HEADER_INCL_IN_TEXT */ | |
836 end = ptr + f_ohdr.tsize; | |
837 write_segment (new, ptr, end); | |
838 | |
839 lseek (new, (long) data_scnptr, 0); | |
840 ptr = (char *) f_ohdr.data_start; | |
841 end = ptr + f_ohdr.dsize; | |
842 write_segment (new, ptr, end); | |
843 | |
844 #endif /* USG_SHARED_LIBRARIES */ | |
845 | |
846 #else /* if not COFF */ | |
847 | |
848 /* Some machines count the header as part of the text segment. | |
849 That is to say, the header appears in core | |
850 just before the address that start_of_text () returns. | |
851 For them, N_TXTOFF is the place where the header goes. | |
852 We must adjust the seek to the place after the header. | |
853 Note that at this point hdr.a_text does *not* count | |
854 the extra A_TEXT_OFFSET bytes, only the actual bytes of code. */ | |
855 | |
856 #ifdef A_TEXT_SEEK | |
857 lseek (new, (long) A_TEXT_SEEK (hdr), 0); | |
858 #else | |
859 lseek (new, (long) N_TXTOFF (hdr), 0); | |
860 #endif /* no A_TEXT_SEEK */ | |
861 | |
862 ptr = (char *) unexec_text_start; | |
863 end = ptr + hdr.a_text; | |
864 write_segment (new, ptr, end); | |
865 | |
866 ptr = (char *) unexec_data_start; | |
867 end = ptr + hdr.a_data; | |
868 /* This lseek is certainly incorrect when A_TEXT_OFFSET | |
869 and I believe it is a no-op otherwise. | |
870 Let's see if its absence ever fails. */ | |
871 /* lseek (new, (long) N_TXTOFF (hdr) + hdr.a_text, 0); */ | |
872 write_segment (new, ptr, end); | |
873 | |
874 #endif /* not COFF */ | |
875 | |
876 return 0; | |
877 } | |
878 | |
879 write_segment (new, ptr, end) | |
880 int new; | |
881 register char *ptr, *end; | |
882 { | |
883 register int i, nwrite, ret; | |
884 char buf[80]; | |
885 extern int errno; | |
886 char zeros[128]; | |
887 | |
888 bzero (zeros, sizeof zeros); | |
889 | |
890 for (i = 0; ptr < end;) | |
891 { | |
892 /* distance to next multiple of 128. */ | |
893 nwrite = (((int) ptr + 128) & -128) - (int) ptr; | |
894 /* But not beyond specified end. */ | |
895 if (nwrite > end - ptr) nwrite = end - ptr; | |
896 ret = write (new, ptr, nwrite); | |
897 /* If write gets a page fault, it means we reached | |
898 a gap between the old text segment and the old data segment. | |
899 This gap has probably been remapped into part of the text segment. | |
900 So write zeros for it. */ | |
901 if (ret == -1 && errno == EFAULT) | |
902 write (new, zeros, nwrite); | |
903 else if (nwrite != ret) | |
904 { | |
905 sprintf (buf, | |
906 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d", | |
907 ptr, new, nwrite, ret, errno); | |
908 PERROR (buf); | |
909 } | |
910 i += nwrite; | |
911 ptr += nwrite; | |
912 } | |
913 } | |
914 | |
915 /* **************************************************************** | |
916 * copy_sym | |
917 * | |
918 * Copy the relocation information and symbol table from the a.out to the new | |
919 */ | |
920 static int | |
921 copy_sym (new, a_out, a_name, new_name) | |
922 int new, a_out; | |
923 char *a_name, *new_name; | |
924 { | |
925 char page[1024]; | |
926 int n; | |
927 | |
928 if (a_out < 0) | |
929 return 0; | |
930 | |
931 #ifdef COFF | |
932 if (SYMS_START == 0L) | |
933 return 0; | |
934 #endif /* COFF */ | |
935 | |
936 #ifdef COFF | |
937 if (lnnoptr) /* if there is line number info */ | |
938 lseek (a_out, lnnoptr, 0); /* start copying from there */ | |
939 else | |
940 #endif /* COFF */ | |
941 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */ | |
942 | |
943 while ((n = read (a_out, page, sizeof page)) > 0) | |
944 { | |
945 if (write (new, page, n) != n) | |
946 { | |
947 PERROR (new_name); | |
948 } | |
949 } | |
950 if (n < 0) | |
951 { | |
952 PERROR (a_name); | |
953 } | |
954 return 0; | |
955 } | |
956 | |
957 /* **************************************************************** | |
958 * mark_x | |
959 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2917
diff
changeset
|
960 * After successfully building the new a.out, mark it executable |
172 | 961 */ |
962 static void | |
963 mark_x (name) | |
964 char *name; | |
965 { | |
966 struct stat sbuf; | |
967 int um; | |
968 int new = 0; /* for PERROR */ | |
969 | |
970 um = umask (777); | |
971 umask (um); | |
972 if (stat (name, &sbuf) == -1) | |
973 { | |
974 PERROR (name); | |
975 } | |
976 sbuf.st_mode |= 0111 & ~um; | |
977 if (chmod (name, sbuf.st_mode) == -1) | |
978 PERROR (name); | |
979 } | |
980 | |
981 #ifdef COFF | |
982 #ifndef COFF_BSD_SYMBOLS | |
983 | |
984 /* | |
985 * If the COFF file contains a symbol table and a line number section, | |
986 * then any auxiliary entries that have values for x_lnnoptr must | |
987 * be adjusted by the amount that the line number section has moved | |
988 * in the file (bias computed in make_hdr). The #@$%&* designers of | |
989 * the auxiliary entry structures used the absolute file offsets for | |
990 * the line number entry rather than an offset from the start of the | |
991 * line number section! | |
992 * | |
993 * When I figure out how to scan through the symbol table and pick out | |
994 * the auxiliary entries that need adjustment, this routine will | |
995 * be fixed. As it is now, all such entries are wrong and sdb | |
996 * will complain. Fred Fish, UniSoft Systems Inc. | |
997 */ | |
998 | |
999 /* This function is probably very slow. Instead of reopening the new | |
1000 file for input and output it should copy from the old to the new | |
1001 using the two descriptors already open (WRITEDESC and READDESC). | |
1002 Instead of reading one small structure at a time it should use | |
1003 a reasonable size buffer. But I don't have time to work on such | |
1004 things, so I am installing it as submitted to me. -- RMS. */ | |
1005 | |
1006 adjust_lnnoptrs (writedesc, readdesc, new_name) | |
1007 int writedesc; | |
1008 int readdesc; | |
1009 char *new_name; | |
1010 { | |
1011 register int nsyms; | |
1012 register int new; | |
579 | 1013 #if defined (amdahl_uts) || defined (pfa) |
172 | 1014 SYMENT symentry; |
1015 AUXENT auxentry; | |
1016 #else | |
1017 struct syment symentry; | |
1018 union auxent auxentry; | |
1019 #endif | |
1020 | |
1021 if (!lnnoptr || !f_hdr.f_symptr) | |
1022 return 0; | |
1023 | |
1024 if ((new = open (new_name, 2)) < 0) | |
1025 { | |
1026 PERROR (new_name); | |
1027 return -1; | |
1028 } | |
1029 | |
1030 lseek (new, f_hdr.f_symptr, 0); | |
1031 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++) | |
1032 { | |
1033 read (new, &symentry, SYMESZ); | |
1034 if (symentry.n_numaux) | |
1035 { | |
1036 read (new, &auxentry, AUXESZ); | |
1037 nsyms++; | |
1937
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1038 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400) |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1039 { |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1040 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
|
1041 lseek (new, -AUXESZ, 1); |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1042 write (new, &auxentry, AUXESZ); |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
1043 } |
172 | 1044 } |
1045 } | |
1046 close (new); | |
1047 } | |
1048 | |
1049 #endif /* COFF_BSD_SYMBOLS */ | |
1050 | |
1051 #endif /* COFF */ | |
1052 | |
1053 #endif /* not CANNOT_UNEXEC */ | |
1054 | |
1055 #endif /* not CANNOT_DUMP */ |