0
|
1 /* Unexec for HP 9000 Series 800 machines.
|
|
2 Bob Desinger <hpsemc!bd@hplabs.hp.com>
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not synched with FSF. */
|
|
22
|
|
23 /*
|
|
24
|
|
25 Unexec creates a copy of the old a.out file, and replaces the old data
|
|
26 area with the current data area. When the new file is executed, the
|
|
27 process will see the same data structures and data values that the
|
|
28 original process had when unexec was called.
|
272
|
29
|
0
|
30 Unlike other versions of unexec, this one copies symbol table and
|
|
31 debug information to the new a.out file. Thus, the new a.out file
|
|
32 may be debugged with symbolic debuggers.
|
272
|
33
|
0
|
34 If you fix any bugs in this, I'd like to incorporate your fixes.
|
|
35 Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
|
272
|
36
|
0
|
37 CAVEATS:
|
|
38 This routine saves the current value of all static and external
|
|
39 variables. This means that any data structure that needs to be
|
|
40 initialized must be explicitly reset. Variables will not have their
|
|
41 expected default values.
|
272
|
42
|
0
|
43 Unfortunately, the HP-UX signal handler has internal initialization
|
|
44 flags which are not explicitly reset. Thus, for signals to work in
|
|
45 conjunction with this routine, the following code must executed when
|
|
46 the new process starts up.
|
272
|
47
|
0
|
48 void _sigreturn();
|
|
49 ...
|
|
50 sigsetreturn(_sigreturn);
|
|
51 */
|
|
52
|
|
53
|
|
54 #include <config.h>
|
|
55 #include <stdio.h>
|
|
56 #include <fcntl.h>
|
|
57 #include <errno.h>
|
|
58
|
|
59 #include <a.out.h>
|
|
60
|
|
61 /*
|
|
62 * Minor modification to enable dumping with shared libraries added by
|
|
63 * Dipankar Gupta (dg@hplb.hpl.hp.com). I studied Oliver Laumann's
|
|
64 * more elaborate dynamic loading scheme in ELK while implementing
|
|
65 * this, but don't use any of his machinery.
|
|
66 *
|
|
67 * Stores the BRK value at dump time, and uses the RUN_TIME_REMAP hook
|
|
68 * to break back to the stored value when the dumped executable is restarted.
|
|
69 *
|
|
70 * CAVEATS (addenda):
|
|
71 * 1. Text area of the shlibs are not stored. Thus, if a shared library is
|
|
72 * replaced between the time of dump and execution, all bets are off.
|
|
73 *
|
|
74 * 2. Assumes that the data and bss area are adjacent, which is true of the
|
|
75 * current VM implementation.
|
|
76 *
|
|
77 * 3. Any setup that defines HPUX_USE_SHLIBS *must* also define
|
272
|
78 * RUN_TIME_REMAP.
|
0
|
79 */
|
|
80
|
|
81 #ifdef HPUX_USE_SHLIBS
|
|
82 #include <dl.h> /* User-space dynamic loader entry points */
|
|
83 void Save_Shared_Data();
|
|
84 int run_time_remap();
|
|
85 #endif
|
|
86
|
|
87 #define min(x,y) ( ((x)<(y))?(x):(y) )
|
|
88
|
272
|
89 void write_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr);
|
|
90 void read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr);
|
|
91 void save_data_space (int file, struct header *hdr,
|
|
92 struct som_exec_auxhdr *auxhdr, int size);
|
|
93 void copy_rest (int old, int new);
|
|
94 void copy_file (int old, int new, int size);
|
|
95 void update_file_ptrs(int file, struct header *hdr,
|
|
96 struct som_exec_auxhdr *auxhdr,
|
|
97 unsigned int location, int offset);
|
0
|
98
|
|
99 /* Create a new a.out file, same as old but with current data space */
|
272
|
100 int
|
|
101 unexec(char new_name[], /* name of the new a.out file to be created */
|
|
102 char old_name[], /* name of the old a.out file */
|
|
103 char *new_end_of_text, /* ptr to new edata/etext; NOT USED YET */
|
|
104 int dummy1, int dummy2) /* not used by emacs */
|
0
|
105 {
|
|
106 int old, new;
|
|
107 int old_size, new_size;
|
|
108 struct header hdr;
|
|
109 struct som_exec_auxhdr auxhdr;
|
|
110 long i;
|
272
|
111
|
0
|
112 /* For the greatest flexibility, should create a temporary file in
|
|
113 the same directory as the new file. When everything is complete,
|
|
114 rename the temp file to the new name.
|
|
115 This way, a program could update its own a.out file even while
|
|
116 it is still executing. If problems occur, everything is still
|
|
117 intact. NOT implemented. */
|
272
|
118
|
0
|
119 /* Open the input and output a.out files */
|
|
120 old = open (old_name, O_RDONLY);
|
|
121 if (old < 0)
|
|
122 { perror(old_name); exit(1); }
|
|
123 new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
|
|
124 if (new < 0)
|
|
125 { perror(new_name); exit(1); }
|
272
|
126
|
0
|
127 /* Read the old headers */
|
|
128 read_header(old, &hdr, &auxhdr);
|
272
|
129
|
0
|
130 #ifdef HPUX_USE_SHLIBS
|
|
131 Save_Shared_Data(); /* Save break value (added: dg@hplb.hpl.hp.com) */
|
|
132 #endif
|
|
133 /* Decide how large the new and old data areas are */
|
|
134 old_size = auxhdr.exec_dsize;
|
|
135 /* I suspect these two statements are separate
|
|
136 to avoid a compiler bug in hpux version 8. */
|
|
137 i = (long) sbrk (0);
|
|
138 new_size = i - auxhdr.exec_dmem;
|
272
|
139
|
0
|
140 /* Copy the old file to the new, up to the data space */
|
|
141 lseek(old, 0, 0);
|
|
142 copy_file(old, new, auxhdr.exec_dfile);
|
272
|
143
|
0
|
144 /* Skip the old data segment and write a new one */
|
|
145 lseek(old, old_size, 1);
|
|
146 save_data_space(new, &hdr, &auxhdr, new_size);
|
272
|
147
|
0
|
148 /* Copy the rest of the file */
|
|
149 copy_rest(old, new);
|
272
|
150
|
0
|
151 /* Update file pointers since we probably changed size of data area */
|
|
152 update_file_ptrs(new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
|
272
|
153
|
0
|
154 /* Save the modified header */
|
|
155 write_header(new, &hdr, &auxhdr);
|
272
|
156
|
0
|
157 /* Close the binary file */
|
|
158 close (old);
|
|
159 close (new);
|
|
160 return 0;
|
|
161 }
|
|
162
|
|
163 /* Save current data space in the file, update header. */
|
|
164
|
272
|
165 void
|
|
166 save_data_space (int file, struct header *hdr,
|
|
167 struct som_exec_auxhdr *auxhdr, int size)
|
0
|
168 {
|
|
169 /* Write the entire data space out to the file */
|
|
170 if (write(file, (void *)auxhdr->exec_dmem, size) != size)
|
|
171 { perror("Can't save new data space"); exit(1); }
|
272
|
172
|
0
|
173 /* Update the header to reflect the new data size */
|
|
174 auxhdr->exec_dsize = size;
|
|
175 auxhdr->exec_bsize = 0;
|
|
176 }
|
|
177
|
|
178 /* Update the values of file pointers when something is inserted. */
|
|
179
|
272
|
180 void
|
|
181 update_file_ptrs(int file, struct header *hdr,
|
|
182 struct som_exec_auxhdr *auxhdr,
|
|
183 unsigned int location, int offset)
|
0
|
184 {
|
|
185 struct subspace_dictionary_record subspace;
|
|
186 int i;
|
272
|
187
|
0
|
188 /* Increase the overall size of the module */
|
|
189 hdr->som_length += offset;
|
272
|
190
|
0
|
191 /* Update the various file pointers in the header */
|
|
192 #define update(ptr) if (ptr > location) ptr = ptr + offset
|
|
193 update(hdr->aux_header_location);
|
|
194 update(hdr->space_strings_location);
|
|
195 update(hdr->init_array_location);
|
|
196 update(hdr->compiler_location);
|
|
197 update(hdr->symbol_location);
|
|
198 update(hdr->fixup_request_location);
|
|
199 update(hdr->symbol_strings_location);
|
|
200 update(hdr->unloadable_sp_location);
|
|
201 update(auxhdr->exec_tfile);
|
|
202 update(auxhdr->exec_dfile);
|
272
|
203
|
0
|
204 /* Do for each subspace dictionary entry */
|
|
205 lseek(file, hdr->subspace_location, 0);
|
|
206 for (i = 0; i < hdr->subspace_total; i++)
|
|
207 {
|
|
208 if (read(file, &subspace, sizeof(subspace)) != sizeof(subspace))
|
|
209 { perror("Can't read subspace record"); exit(1); }
|
272
|
210
|
0
|
211 /* If subspace has a file location, update it */
|
272
|
212 if (subspace.initialization_length > 0
|
0
|
213 && subspace.file_loc_init_value > location)
|
|
214 {
|
|
215 subspace.file_loc_init_value += offset;
|
|
216 lseek(file, -sizeof(subspace), 1);
|
|
217 if (write(file, &subspace, sizeof(subspace)) != sizeof(subspace))
|
|
218 { perror("Can't update subspace record"); exit(1); }
|
|
219 }
|
272
|
220 }
|
|
221
|
0
|
222 /* Do for each initialization pointer record */
|
|
223 /* (I don't think it applies to executable files, only relocatables) */
|
|
224 #undef update
|
|
225 }
|
|
226
|
|
227 /* Read in the header records from an a.out file. */
|
|
228
|
272
|
229 void
|
|
230 read_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
|
0
|
231 {
|
272
|
232
|
0
|
233 /* Read the header in */
|
|
234 lseek(file, 0, 0);
|
|
235 if (read(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
|
|
236 { perror("Couldn't read header from a.out file"); exit(1); }
|
272
|
237
|
0
|
238 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
|
|
239 && hdr->a_magic != DEMAND_MAGIC)
|
|
240 {
|
272
|
241 fprintf(stderr, "a.out file doesn't have legal magic number\n");
|
|
242 exit(1);
|
0
|
243 }
|
272
|
244
|
0
|
245 lseek(file, hdr->aux_header_location, 0);
|
|
246 if (read(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
|
|
247 {
|
|
248 perror("Couldn't read auxiliary header from a.out file");
|
|
249 exit(1);
|
272
|
250 }
|
0
|
251 }
|
|
252
|
|
253 /* Write out the header records into an a.out file. */
|
272
|
254 void
|
|
255 write_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
|
0
|
256 {
|
|
257 /* Update the checksum */
|
|
258 hdr->checksum = calculate_checksum(hdr);
|
272
|
259
|
0
|
260 /* Write the header back into the a.out file */
|
|
261 lseek(file, 0, 0);
|
|
262 if (write(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
|
|
263 { perror("Couldn't write header to a.out file"); exit(1); }
|
|
264 lseek(file, hdr->aux_header_location, 0);
|
|
265 if (write(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
|
|
266 { perror("Couldn't write auxiliary header to a.out file"); exit(1); }
|
|
267 }
|
|
268
|
|
269 /* Calculate the checksum of a SOM header record. */
|
272
|
270 int
|
|
271 calculate_checksum(struct header *hdr)
|
0
|
272 {
|
|
273 int checksum, i, *ptr;
|
272
|
274
|
0
|
275 checksum = 0; ptr = (int *) hdr;
|
272
|
276
|
0
|
277 for (i=0; i<sizeof(*hdr)/sizeof(int)-1; i++)
|
|
278 checksum ^= ptr[i];
|
272
|
279
|
0
|
280 return(checksum);
|
|
281 }
|
|
282
|
|
283 /* Copy size bytes from the old file to the new one. */
|
272
|
284 void
|
|
285 copy_file (int old, int new, int size)
|
0
|
286 {
|
|
287 int len;
|
|
288 int buffer[8192]; /* word aligned will be faster */
|
272
|
289
|
0
|
290 for (; size > 0; size -= len)
|
|
291 {
|
|
292 len = min(size, sizeof(buffer));
|
|
293 if (read(old, buffer, len) != len)
|
|
294 { perror("Read failure on a.out file"); exit(1); }
|
|
295 if (write(new, buffer, len) != len)
|
|
296 { perror("Write failure in a.out file"); exit(1); }
|
|
297 }
|
|
298 }
|
|
299
|
|
300 /* Copy the rest of the file, up to EOF. */
|
272
|
301 void
|
|
302 copy_rest (int old, int new)
|
0
|
303 {
|
|
304 int buffer[4096];
|
|
305 int len;
|
272
|
306
|
0
|
307 /* Copy bytes until end of file or error */
|
|
308 while ( (len = read(old, buffer, sizeof(buffer))) > 0)
|
|
309 if (write(new, buffer, len) != len) break;
|
272
|
310
|
0
|
311 if (len != 0)
|
|
312 { perror("Unable to copy the rest of the file"); exit(1); }
|
|
313 }
|
|
314
|
|
315 #ifdef DEBUG
|
272
|
316 display_header(struct header *hdr, struct som_exec_auxhdr *auxhdr)
|
0
|
317 {
|
|
318 /* Display the header information (debug) */
|
|
319 printf("\n\nFILE HEADER\n");
|
272
|
320 printf("magic number %d \n", hdr->a_magic);
|
0
|
321 printf("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
|
|
322 printf("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
|
|
323 printf("entry %x \n", auxhdr->exec_entry);
|
|
324 printf("Bss segment size %u\n", auxhdr->exec_bsize);
|
|
325 printf("\n");
|
|
326 printf("data file loc %d size %d\n",
|
|
327 auxhdr->exec_dfile, auxhdr->exec_dsize);
|
|
328 printf("som_length %d\n", hdr->som_length);
|
|
329 printf("unloadable sploc %d size %d\n",
|
|
330 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
|
|
331 }
|
|
332 #endif /* DEBUG */
|
|
333
|
|
334 #ifdef HPUX_USE_SHLIBS
|
|
335 /* Added machinery for shared libs... see comments at the beginning of this file. */
|
|
336
|
|
337 void *Brk_On_Dump = 0; /* Brk value to restore... stored as a global */
|
|
338
|
|
339 void Save_Shared_Data () {
|
|
340 Brk_On_Dump = sbrk( 0 );
|
|
341 }
|
272
|
342
|
0
|
343 void Restore_Shared_Data () {
|
|
344 brk ( Brk_On_Dump );
|
|
345 }
|
|
346
|
|
347 int run_time_remap (int d) {
|
|
348 Restore_Shared_Data();
|
|
349 }
|
|
350
|
|
351 /* run_time_remap is the magic called by startup code in the dumped executable
|
272
|
352 * if RUN_TIME_REMAP is set.
|
0
|
353 */
|
|
354 #endif /* HPUX_USE_SHLIBS */
|