Mercurial > emacs
annotate src/unexcoff.c @ 109885:42a68d95e799
* image.c (imagemagick_clear_image): Remove debugging output.
author | Andreas Schwab <schwab@linux-m68k.org> |
---|---|
date | Fri, 20 Aug 2010 14:02:43 +0200 |
parents | 8186d147fb99 |
children | fe9b13a5f44f 3226ac2da7f7 adb097c7df58 |
rev | line source |
---|---|
75227
e90d04cd455a
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
68651
diff
changeset
|
1 /* Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 2001, 2002, 2003, |
106815 | 2 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
172 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
94963
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91702
diff
changeset
|
6 GNU Emacs is free software: you can redistribute it and/or modify |
172 | 7 it under the terms of the GNU General Public License as published by |
94963
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91702
diff
changeset
|
8 the Free Software Foundation, either version 3 of the License, or |
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91702
diff
changeset
|
9 (at your option) any later version. |
172 | 10 |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
94963
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91702
diff
changeset
|
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
172 | 18 |
19 | |
20 /* | |
109628
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
21 * unexcoff.c - Convert a running program into an a.out or COFF file. |
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
22 * |
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
23 * ================================================================== |
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
24 * Note: This file is currently used only by the MSDOS (a.k.a. DJGPP) |
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
25 * build of Emacs. If you are not interested in the MSDOS build, you |
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
26 * are looking at the wrong version of unexec! |
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
27 * ================================================================== |
172 | 28 * |
29 * Author: Spencer W. Thomas | |
30 * Computer Science Dept. | |
31 * University of Utah | |
32 * Date: Tue Mar 2 1982 | |
109628
8e2c7c31861b
Rename src/unexec.c => src/unexcoff.c.
Eli Zaretskii <eliz@gnu.org>
parents:
109570
diff
changeset
|
33 * Originally under the name unexec.c. |
172 | 34 * Modified heavily since then. |
35 * | |
36 * Synopsis: | |
37 * unexec (new_name, a_name, data_start, bss_start, entry_address) | |
38 * char *new_name, *a_name; | |
39 * unsigned data_start, bss_start, entry_address; | |
40 * | |
41 * Takes a snapshot of the program and makes an a.out format file in the | |
42 * file named by the string argument new_name. | |
43 * If a_name is non-NULL, the symbol table will be taken from the given file. | |
44 * On some machines, an existing a_name file is required. | |
45 * | |
46 * The boundaries within the a.out file may be adjusted with the data_start | |
47 * and bss_start arguments. Either or both may be given as 0 for defaults. | |
48 * | |
49 * Data_start gives the boundary between the text segment and the data | |
50 * segment of the program. The text segment can contain shared, read-only | |
51 * program code and literal data, while the data segment is always unshared | |
52 * and unprotected. Data_start gives the lowest unprotected address. | |
53 * The value you specify may be rounded down to a suitable boundary | |
54 * as required by the machine you are using. | |
55 * | |
56 * Specifying zero for data_start means the boundary between text and data | |
57 * should not be the same as when the program was loaded. | |
58 * | |
59 * Bss_start indicates how much of the data segment is to be saved in the | |
60 * a.out file and restored when the program is executed. It gives the lowest | |
61 * unsaved address, and is rounded up to a page boundary. The default when 0 | |
62 * is given assumes that the entire data segment is to be stored, including | |
63 * the previous data and bss as well as any additional storage allocated with | |
64 * break (2). | |
65 * | |
66 * The new file is set up to start at entry_address. | |
67 * | |
68 * If you make improvements I'd like to get them too. | |
69 * harpo!utah-cs!thomas, thomas@Utah-20 | |
70 * | |
71 */ | |
72 | |
73 /* Modified to support SysVr3 shared libraries by James Van Artsdalen | |
74 * of Dell Computer Corporation. james@bigtex.cactus.org. | |
75 */ | |
76 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
4319
diff
changeset
|
77 #include <config.h> |
172 | 78 #define PERROR(file) report_error (file, new) |
79 | |
80 #ifndef CANNOT_DUMP /* all rest of file! */ | |
81 | |
96926
baf6162e41d4
Remove code depending on !COFF and USG, the file is
Dan Nicolaescu <dann@ics.uci.edu>
parents:
96732
diff
changeset
|
82 #ifdef HAVE_COFF_H |
29650
2411aacca614
(toplevel) [COFF]: Include coff.h.
Gerd Moellmann <gerd@gnu.org>
parents:
22647
diff
changeset
|
83 #include <coff.h> |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
84 #ifdef MSDOS |
14975
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
85 #include <fcntl.h> /* for O_RDONLY, O_RDWR */ |
15732
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
86 #include <crt0.h> /* for _crt0_startup_flags and its bits */ |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
87 static int save_djgpp_startup_flags; |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
88 #define filehdr external_filehdr |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
89 #define scnhdr external_scnhdr |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
90 #define syment external_syment |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
91 #define auxent external_auxent |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
92 #define n_numaux e_numaux |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
93 #define n_type e_type |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
94 struct aouthdr |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
95 { |
7626
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
96 unsigned short magic; /* type of file */ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
97 unsigned short vstamp; /* version stamp */ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
98 unsigned long tsize; /* text size in bytes, padded to FW bdry*/ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
99 unsigned long dsize; /* initialized data " " */ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
100 unsigned long bsize; /* uninitialized data " " */ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
101 unsigned long entry; /* entry pt. */ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
102 unsigned long text_start;/* base of text used for this file */ |
7ae305576201
[MSDOS]: Don't include files from the dos extender
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
103 unsigned long data_start;/* base of data used for this file */ |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
104 }; |
29668
1e6eeead2f1d
(toplevel): Fix last change, so as not to deprive MSDOS
Eli Zaretskii <eliz@gnu.org>
parents:
29650
diff
changeset
|
105 #endif /* not MSDOS */ |
96926
baf6162e41d4
Remove code depending on !COFF and USG, the file is
Dan Nicolaescu <dann@ics.uci.edu>
parents:
96732
diff
changeset
|
106 #else /* not HAVE_COFF_H */ |
172 | 107 #include <a.out.h> |
96926
baf6162e41d4
Remove code depending on !COFF and USG, the file is
Dan Nicolaescu <dann@ics.uci.edu>
parents:
96732
diff
changeset
|
108 #endif /* not HAVE_COFF_H */ |
485 | 109 |
9699 | 110 /* Define getpagesize if the system does not. |
111 Note that this may depend on symbols defined in a.out.h. */ | |
172 | 112 #include "getpagesize.h" |
113 | |
114 #ifndef makedev /* Try to detect types.h already loaded */ | |
115 #include <sys/types.h> | |
485 | 116 #endif /* makedev */ |
172 | 117 #include <stdio.h> |
118 #include <sys/stat.h> | |
119 #include <errno.h> | |
120 | |
96926
baf6162e41d4
Remove code depending on !COFF and USG, the file is
Dan Nicolaescu <dann@ics.uci.edu>
parents:
96732
diff
changeset
|
121 #include <sys/file.h> |
7921
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
122 |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
123 #ifndef O_RDONLY |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
124 #define O_RDONLY 0 |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
125 #endif |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
126 #ifndef O_RDWR |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
127 #define O_RDWR 2 |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
128 #endif |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
129 |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
130 |
172 | 131 extern char *start_of_data (); /* Start of initialized data */ |
132 | |
133 static long block_copy_start; /* Old executable start point */ | |
134 static struct filehdr f_hdr; /* File header */ | |
135 static struct aouthdr f_ohdr; /* Optional file header (a.out) */ | |
136 long bias; /* Bias to add for growth */ | |
137 long lnnoptr; /* Pointer to line-number info within file */ | |
138 #define SYMS_START block_copy_start | |
139 | |
140 static long text_scnptr; | |
141 static long data_scnptr; | |
142 | |
22647
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
143 static long coff_offset; |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
144 |
172 | 145 static int pagemask; |
146 | |
147 /* Correct an int which is the bit pattern of a pointer to a byte | |
148 into an int which is the number of a byte. | |
149 This is a no-op on ordinary machines, but not on all. */ | |
150 | |
151 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0) | |
152 | |
105669
68dd71358159
* alloc.c: Do not define struct catchtag.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
105426
diff
changeset
|
153 #include <setjmp.h> |
7921
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
154 #include "lisp.h" |
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
155 |
172 | 156 static |
157 report_error (file, fd) | |
158 char *file; | |
159 int fd; | |
160 { | |
161 if (fd) | |
162 close (fd); | |
7921
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
163 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil)); |
172 | 164 } |
165 | |
166 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1 | |
167 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1 | |
168 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1 | |
169 | |
170 static | |
171 report_error_1 (fd, msg, a1, a2) | |
172 int fd; | |
173 char *msg; | |
174 int a1, a2; | |
175 { | |
176 close (fd); | |
177 error (msg, a1, a2); | |
178 } | |
179 | |
180 static int make_hdr (); | |
181 static int copy_text_and_data (); | |
182 static int copy_sym (); | |
183 static void mark_x (); | |
184 | |
185 /* **************************************************************** | |
186 * make_hdr | |
187 * | |
188 * Make the header in the new a.out from the header in core. | |
189 * Modify the text and data sizes. | |
190 */ | |
191 static int | |
192 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) | |
193 int new, a_out; | |
194 unsigned data_start, bss_start, entry_address; | |
195 char *a_name; | |
196 char *new_name; | |
197 { | |
198 int tem; | |
199 auto struct scnhdr f_thdr; /* Text section header */ | |
200 auto struct scnhdr f_dhdr; /* Data section header */ | |
201 auto struct scnhdr f_bhdr; /* Bss section header */ | |
202 auto struct scnhdr scntemp; /* Temporary section header */ | |
203 register int scns; | |
204 unsigned int bss_end; | |
205 | |
206 pagemask = getpagesize () - 1; | |
207 | |
208 /* Adjust text/data boundary. */ | |
209 data_start = (int) start_of_data (); | |
210 data_start = ADDR_CORRECT (data_start); | |
211 data_start = data_start & ~pagemask; /* (Down) to page boundary. */ | |
212 | |
213 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask; | |
214 bss_end &= ~ pagemask; | |
215 | |
216 /* Adjust data/bss boundary. */ | |
217 if (bss_start != 0) | |
218 { | |
219 bss_start = (ADDR_CORRECT (bss_start) + pagemask); | |
220 /* (Up) to page bdry. */ | |
221 bss_start &= ~ pagemask; | |
222 if (bss_start > bss_end) | |
223 { | |
224 ERROR1 ("unexec: Specified bss_start (%u) is past end of program", | |
225 bss_start); | |
226 } | |
227 } | |
228 else | |
229 bss_start = bss_end; | |
230 | |
231 if (data_start > bss_start) /* Can't have negative data size. */ | |
232 { | |
233 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)", | |
234 data_start, bss_start); | |
235 } | |
236 | |
22647
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
237 coff_offset = 0L; /* stays zero, except in DJGPP */ |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
238 |
172 | 239 /* Salvage as much info from the existing file as possible */ |
240 if (a_out >= 0) | |
241 { | |
22647
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
242 #ifdef MSDOS |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
243 /* Support the coff-go32-exe format with a prepended stub, since |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
244 this is what GCC 2.8.0 and later generates by default in DJGPP. */ |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
245 unsigned short mz_header[3]; |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
246 |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
247 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header)) |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
248 { |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
249 PERROR (a_name); |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
250 } |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
251 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */ |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
252 { |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
253 coff_offset = (long)mz_header[2] * 512L; |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
254 if (mz_header[1]) |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
255 coff_offset += (long)mz_header[1] - 512L; |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
256 lseek (a_out, coff_offset, 0); |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
257 } |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
258 else |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
259 lseek (a_out, 0L, 0); |
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
260 #endif /* MSDOS */ |
172 | 261 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) |
262 { | |
263 PERROR (a_name); | |
264 } | |
265 block_copy_start += sizeof (f_hdr); | |
266 if (f_hdr.f_opthdr > 0) | |
267 { | |
268 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
269 { | |
270 PERROR (a_name); | |
271 } | |
272 block_copy_start += sizeof (f_ohdr); | |
273 } | |
274 /* Loop through section headers, copying them in */ | |
22647
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
275 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0); |
172 | 276 for (scns = f_hdr.f_nscns; scns > 0; scns--) { |
277 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp)) | |
278 { | |
279 PERROR (a_name); | |
280 } | |
281 if (scntemp.s_scnptr > 0L) | |
282 { | |
283 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size) | |
284 block_copy_start = scntemp.s_scnptr + scntemp.s_size; | |
285 } | |
286 if (strcmp (scntemp.s_name, ".text") == 0) | |
287 { | |
288 f_thdr = scntemp; | |
289 } | |
290 else if (strcmp (scntemp.s_name, ".data") == 0) | |
291 { | |
292 f_dhdr = scntemp; | |
293 } | |
294 else if (strcmp (scntemp.s_name, ".bss") == 0) | |
295 { | |
296 f_bhdr = scntemp; | |
297 } | |
298 } | |
299 } | |
300 else | |
301 { | |
302 ERROR0 ("can't build a COFF file from scratch yet"); | |
303 } | |
304 | |
305 /* Now we alter the contents of all the f_*hdr variables | |
306 to correspond to what we want to dump. */ | |
307 | |
308 f_hdr.f_flags |= (F_RELFLG | F_EXEC); | |
309 f_ohdr.dsize = bss_start - f_ohdr.data_start; | |
310 f_ohdr.bsize = bss_end - bss_start; | |
311 f_thdr.s_size = f_ohdr.tsize; | |
312 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr); | |
313 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr)); | |
314 lnnoptr = f_thdr.s_lnnoptr; | |
315 text_scnptr = f_thdr.s_scnptr; | |
316 f_dhdr.s_paddr = f_ohdr.data_start; | |
317 f_dhdr.s_vaddr = f_ohdr.data_start; | |
318 f_dhdr.s_size = f_ohdr.dsize; | |
319 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size; | |
320 data_scnptr = f_dhdr.s_scnptr; | |
321 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize; | |
322 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize; | |
323 f_bhdr.s_size = f_ohdr.bsize; | |
324 f_bhdr.s_scnptr = 0L; | |
325 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start; | |
326 | |
327 if (f_hdr.f_symptr > 0L) | |
328 { | |
329 f_hdr.f_symptr += bias; | |
330 } | |
331 | |
332 if (f_thdr.s_lnnoptr > 0L) | |
333 { | |
334 f_thdr.s_lnnoptr += bias; | |
335 } | |
336 | |
337 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | |
338 { | |
339 PERROR (new_name); | |
340 } | |
341 | |
342 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) | |
343 { | |
344 PERROR (new_name); | |
345 } | |
346 | |
347 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr)) | |
348 { | |
349 PERROR (new_name); | |
350 } | |
351 | |
352 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr)) | |
353 { | |
354 PERROR (new_name); | |
355 } | |
356 | |
357 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr)) | |
358 { | |
359 PERROR (new_name); | |
360 } | |
361 | |
362 return (0); | |
363 | |
364 } | |
365 | |
60728
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
366 write_segment (new, ptr, end) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
367 int new; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
368 register char *ptr, *end; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
369 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
370 register int i, nwrite, ret; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
371 char buf[80]; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
372 /* This is the normal amount to write at once. |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
373 It is the size of block that NFS uses. */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
374 int writesize = 1 << 13; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
375 int pagesize = getpagesize (); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
376 char zeros[1 << 13]; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
377 |
109165
750db9f3e6d8
Replace bcopy, bzero, bcmp by memcpy, memmove, memset, memcmp
Andreas Schwab <schwab@linux-m68k.org>
parents:
107737
diff
changeset
|
378 memset (zeros, 0, sizeof (zeros)); |
60728
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
379 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
380 for (i = 0; ptr < end;) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
381 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
382 /* Distance to next multiple of writesize. */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
383 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
384 /* But not beyond specified end. */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
385 if (nwrite > end - ptr) nwrite = end - ptr; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
386 ret = write (new, ptr, nwrite); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
387 /* If write gets a page fault, it means we reached |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
388 a gap between the old text segment and the old data segment. |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
389 This gap has probably been remapped into part of the text segment. |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
390 So write zeros for it. */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
391 if (ret == -1 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
392 #ifdef EFAULT |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
393 && errno == EFAULT |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
394 #endif |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
395 ) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
396 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
397 /* Write only a page of zeros at once, |
105426
d0a6d64c3cfc
Fix typos in comments.
Juanma Barranquero <lekktu@gmail.com>
parents:
100951
diff
changeset
|
398 so that we don't overshoot the start |
60728
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
399 of the valid memory in the old data segment. */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
400 if (nwrite > pagesize) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
401 nwrite = pagesize; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
402 write (new, zeros, nwrite); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
403 } |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
404 i += nwrite; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
405 ptr += nwrite; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
406 } |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
407 } |
172 | 408 /* **************************************************************** |
409 * copy_text_and_data | |
410 * | |
411 * Copy the text and data segments from memory to the new a.out | |
412 */ | |
413 static int | |
414 copy_text_and_data (new, a_out) | |
415 int new, a_out; | |
416 { | |
417 register char *end; | |
418 register char *ptr; | |
419 | |
14975
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
420 #ifdef MSDOS |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
421 /* Dump the original table of exception handlers, not the one |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
422 where our exception hooks are registered. */ |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
423 __djgpp_exception_toggle (); |
15732
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
424 |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
425 /* Switch off startup flags that might have been set at runtime |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
426 and which might change the way that dumped Emacs works. */ |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
427 save_djgpp_startup_flags = _crt0_startup_flags; |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
428 _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR); |
14975
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
429 #endif |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
430 |
172 | 431 lseek (new, (long) text_scnptr, 0); |
432 ptr = (char *) f_ohdr.text_start; | |
433 end = ptr + f_ohdr.tsize; | |
434 write_segment (new, ptr, end); | |
435 | |
436 lseek (new, (long) data_scnptr, 0); | |
437 ptr = (char *) f_ohdr.data_start; | |
438 end = ptr + f_ohdr.dsize; | |
439 write_segment (new, ptr, end); | |
440 | |
14975
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
441 #ifdef MSDOS |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
442 /* Restore our exception hooks. */ |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
443 __djgpp_exception_toggle (); |
15732
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
444 |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
445 /* Restore the startup flags. */ |
c24b00e705ba
(copy_text_and_data) [DJGPP >= 2]: Switch off two bits
Karl Heuer <kwzh@gnu.org>
parents:
14975
diff
changeset
|
446 _crt0_startup_flags = save_djgpp_startup_flags; |
14975
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
447 #endif |
7b91ceb19771
[DJGPP v2]: Include fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
448 |
172 | 449 |
450 return 0; | |
451 } | |
452 | |
453 /* **************************************************************** | |
454 * copy_sym | |
455 * | |
456 * Copy the relocation information and symbol table from the a.out to the new | |
457 */ | |
458 static int | |
459 copy_sym (new, a_out, a_name, new_name) | |
460 int new, a_out; | |
461 char *a_name, *new_name; | |
462 { | |
463 char page[1024]; | |
464 int n; | |
465 | |
466 if (a_out < 0) | |
467 return 0; | |
468 | |
469 if (SYMS_START == 0L) | |
470 return 0; | |
471 | |
472 if (lnnoptr) /* if there is line number info */ | |
22647
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
473 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */ |
172 | 474 else |
22647
1f418e353dd7
[COFF]: New variable coff_offset.
Richard M. Stallman <rms@gnu.org>
parents:
15732
diff
changeset
|
475 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */ |
172 | 476 |
477 while ((n = read (a_out, page, sizeof page)) > 0) | |
478 { | |
479 if (write (new, page, n) != n) | |
480 { | |
481 PERROR (new_name); | |
482 } | |
483 } | |
484 if (n < 0) | |
485 { | |
486 PERROR (a_name); | |
487 } | |
488 return 0; | |
489 } | |
490 | |
491 /* **************************************************************** | |
492 * mark_x | |
493 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2917
diff
changeset
|
494 * After successfully building the new a.out, mark it executable |
172 | 495 */ |
496 static void | |
497 mark_x (name) | |
498 char *name; | |
499 { | |
500 struct stat sbuf; | |
501 int um; | |
502 int new = 0; /* for PERROR */ | |
503 | |
504 um = umask (777); | |
505 umask (um); | |
506 if (stat (name, &sbuf) == -1) | |
507 { | |
508 PERROR (name); | |
509 } | |
510 sbuf.st_mode |= 0111 & ~um; | |
511 if (chmod (name, sbuf.st_mode) == -1) | |
512 PERROR (name); | |
513 } | |
514 | |
515 | |
516 /* | |
517 * If the COFF file contains a symbol table and a line number section, | |
518 * then any auxiliary entries that have values for x_lnnoptr must | |
519 * be adjusted by the amount that the line number section has moved | |
520 * in the file (bias computed in make_hdr). The #@$%&* designers of | |
521 * the auxiliary entry structures used the absolute file offsets for | |
522 * the line number entry rather than an offset from the start of the | |
523 * line number section! | |
524 * | |
525 * When I figure out how to scan through the symbol table and pick out | |
526 * the auxiliary entries that need adjustment, this routine will | |
527 * be fixed. As it is now, all such entries are wrong and sdb | |
528 * will complain. Fred Fish, UniSoft Systems Inc. | |
529 */ | |
530 | |
531 /* This function is probably very slow. Instead of reopening the new | |
532 file for input and output it should copy from the old to the new | |
533 using the two descriptors already open (WRITEDESC and READDESC). | |
534 Instead of reading one small structure at a time it should use | |
535 a reasonable size buffer. But I don't have time to work on such | |
536 things, so I am installing it as submitted to me. -- RMS. */ | |
537 | |
538 adjust_lnnoptrs (writedesc, readdesc, new_name) | |
539 int writedesc; | |
540 int readdesc; | |
541 char *new_name; | |
542 { | |
543 register int nsyms; | |
544 register int new; | |
545 struct syment symentry; | |
546 union auxent auxentry; | |
547 | |
548 if (!lnnoptr || !f_hdr.f_symptr) | |
549 return 0; | |
550 | |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
551 #ifdef MSDOS |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
552 if ((new = writedesc) < 0) |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
553 #else |
7921
b3a5b629fe26
Include <sys/file.h> and [USG5] <fcntl.h> to define O_* macros.
Roland McGrath <roland@gnu.org>
parents:
7626
diff
changeset
|
554 if ((new = open (new_name, O_RDWR)) < 0) |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
555 #endif |
172 | 556 { |
557 PERROR (new_name); | |
558 return -1; | |
559 } | |
560 | |
561 lseek (new, f_hdr.f_symptr, 0); | |
562 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++) | |
563 { | |
564 read (new, &symentry, SYMESZ); | |
565 if (symentry.n_numaux) | |
566 { | |
567 read (new, &auxentry, AUXESZ); | |
568 nsyms++; | |
1937
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
569 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400) |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
570 { |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
571 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
|
572 lseek (new, -AUXESZ, 1); |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
573 write (new, &auxentry, AUXESZ); |
087889e85644
(Fforward_comment): New function.
Richard M. Stallman <rms@gnu.org>
parents:
620
diff
changeset
|
574 } |
172 | 575 } |
576 } | |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
577 #ifndef MSDOS |
172 | 578 close (new); |
5500
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
579 #endif |
6f6637309b38
[MSDOS]: Don't #include <a.out.h>, but use other headers.
Richard M. Stallman <rms@gnu.org>
parents:
4973
diff
changeset
|
580 return 0; |
172 | 581 } |
582 | |
60728
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
583 /* **************************************************************** |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
584 * unexec |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
585 * |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
586 * driving logic. |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
587 */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
588 unexec (new_name, a_name, data_start, bss_start, entry_address) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
589 char *new_name, *a_name; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
590 unsigned data_start, bss_start, entry_address; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
591 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
592 int new, a_out = -1; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
593 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
594 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
595 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
596 PERROR (a_name); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
597 } |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
598 if ((new = creat (new_name, 0666)) < 0) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
599 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
600 PERROR (new_name); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
601 } |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
602 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
603 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
604 || copy_text_and_data (new, a_out) < 0 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
605 || copy_sym (new, a_out, a_name, new_name) < 0 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
606 || adjust_lnnoptrs (new, a_out, new_name) < 0 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
607 ) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
608 { |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
609 close (new); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
610 /* unlink (new_name); /* Failed, unlink new a.out */ |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
611 return -1; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
612 } |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
613 |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
614 close (new); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
615 if (a_out >= 0) |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
616 close (a_out); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
617 mark_x (new_name); |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
618 return 0; |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
619 } |
1e22d789c8c7
(write_segment, unexec): Move these functions to avoid forward
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
620 |
172 | 621 #endif /* not CANNOT_DUMP */ |
52401 | 622 |
623 /* arch-tag: 62409b69-e27a-4a7c-9413-0210d6b54e7f | |
624 (do not change this comment) */ |