Mercurial > hg > xemacs-beta
annotate src/unexhp9k800.c @ 5450:8c406331e77e
Copyright and license to src/s/sol2.h.
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Thu, 06 Jan 2011 01:31:43 +0100 |
parents | 308d34e9f07d |
children | 90dcf2376909 |
rev | line source |
---|---|
428 | 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 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3025
diff
changeset
|
6 XEmacs is free software: you can redistribute it and/or modify it |
428 | 7 under the terms of the GNU General Public License as published by the |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3025
diff
changeset
|
8 Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3025
diff
changeset
|
9 option) any later version. |
428 | 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 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3025
diff
changeset
|
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 18 |
19 /* Synched up with: Not synched with FSF. */ | |
20 | |
21 /* | |
22 | |
23 Unexec creates a copy of the old a.out file, and replaces the old data | |
24 area with the current data area. When the new file is executed, the | |
25 process will see the same data structures and data values that the | |
26 original process had when unexec was called. | |
27 | |
28 Unlike other versions of unexec, this one copies symbol table and | |
29 debug information to the new a.out file. Thus, the new a.out file | |
30 may be debugged with symbolic debuggers. | |
31 | |
32 If you fix any bugs in this, I'd like to incorporate your fixes. | |
33 Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM. | |
34 | |
35 CAVEATS: | |
36 This routine saves the current value of all static and external | |
37 variables. This means that any data structure that needs to be | |
38 initialized must be explicitly reset. Variables will not have their | |
39 expected default values. | |
40 | |
41 Unfortunately, the HP-UX signal handler has internal initialization | |
42 flags which are not explicitly reset. Thus, for signals to work in | |
43 conjunction with this routine, the following code must executed when | |
44 the new process starts up. | |
45 | |
46 void _sigreturn(); | |
47 ... | |
48 sigsetreturn(_sigreturn); | |
49 */ | |
50 | |
51 | |
52 #include <config.h> | |
430 | 53 #include <stdlib.h> |
428 | 54 #include <stdio.h> |
55 #include <fcntl.h> | |
56 #include <errno.h> | |
57 | |
58 #include <a.out.h> | |
442 | 59 #include "lisp.h" |
428 | 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 | |
78 * RUN_TIME_REMAP. | |
79 */ | |
80 | |
81 #ifdef HPUX_USE_SHLIBS | |
82 #include <dl.h> /* User-space dynamic loader entry points */ | |
442 | 83 static void Save_Shared_Data (void); |
84 static void Restore_Shared_Data (void); | |
428 | 85 #endif |
86 | |
87 void write_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr); | |
88 void read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr); | |
89 void save_data_space (int file, struct header *hdr, | |
90 struct som_exec_auxhdr *auxhdr, int size); | |
3025 | 91 void copy_rest (int old, int new_); |
92 void copy_file (int old, int new_, int size); | |
428 | 93 void update_file_ptrs(int file, struct header *hdr, |
94 struct som_exec_auxhdr *auxhdr, | |
95 unsigned int location, int offset); | |
96 int calculate_checksum(struct header *hdr); | |
97 | |
98 /* Create a new a.out file, same as old but with current data space */ | |
99 int | |
442 | 100 unexec (char *new_name, /* name of the new a.out file to be created */ |
101 char *old_name, /* name of the old a.out file */ | |
2286 | 102 uintptr_t UNUSED (new_end_of_text), /* ptr to new edata/etext */ |
103 uintptr_t UNUSED (dummy1), /* not used by emacs */ | |
104 uintptr_t UNUSED (dummy2)) | |
428 | 105 { |
3025 | 106 int old, new_; |
428 | 107 int old_size, new_size; |
108 struct header hdr; | |
109 struct som_exec_auxhdr auxhdr; | |
110 long i; | |
111 | |
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. */ | |
118 | |
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); } | |
3025 | 123 new_ = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777); |
124 if (new_ < 0) | |
428 | 125 { perror(new_name); exit(1); } |
126 | |
127 /* Read the old headers */ | |
128 read_header(old, &hdr, &auxhdr); | |
129 | |
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; | |
139 | |
140 /* Copy the old file to the new, up to the data space */ | |
141 lseek(old, 0, 0); | |
3025 | 142 copy_file(old, new_, auxhdr.exec_dfile); |
428 | 143 |
144 /* Skip the old data segment and write a new one */ | |
145 lseek(old, old_size, 1); | |
3025 | 146 save_data_space(new_, &hdr, &auxhdr, new_size); |
428 | 147 |
148 /* Copy the rest of the file */ | |
3025 | 149 copy_rest(old, new_); |
428 | 150 |
151 /* Update file pointers since we probably changed size of data area */ | |
3025 | 152 update_file_ptrs(new_, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size); |
428 | 153 |
154 /* Save the modified header */ | |
3025 | 155 write_header(new_, &hdr, &auxhdr); |
428 | 156 |
157 /* Close the binary file */ | |
158 close (old); | |
3025 | 159 close (new_); |
428 | 160 return 0; |
161 } | |
162 | |
163 /* Save current data space in the file, update header. */ | |
164 | |
165 void | |
2286 | 166 save_data_space (int file, struct header *UNUSED (hdr), |
428 | 167 struct som_exec_auxhdr *auxhdr, int size) |
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); } | |
172 | |
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 | |
180 void | |
181 update_file_ptrs(int file, struct header *hdr, | |
182 struct som_exec_auxhdr *auxhdr, | |
183 unsigned int location, int offset) | |
184 { | |
185 struct subspace_dictionary_record subspace; | |
186 int i; | |
187 | |
188 /* Increase the overall size of the module */ | |
189 hdr->som_length += offset; | |
190 | |
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); | |
203 | |
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); } | |
210 | |
211 /* If subspace has a file location, update it */ | |
212 if (subspace.initialization_length > 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 } | |
220 } | |
221 | |
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 | |
229 void | |
230 read_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr) | |
231 { | |
232 | |
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); } | |
237 | |
238 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC | |
239 && hdr->a_magic != DEMAND_MAGIC) | |
240 { | |
241 fprintf(stderr, "a.out file doesn't have legal magic number\n"); | |
242 exit(1); | |
243 } | |
244 | |
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); | |
250 } | |
251 } | |
252 | |
253 /* Write out the header records into an a.out file. */ | |
254 void | |
255 write_header(int file, struct header *hdr, struct som_exec_auxhdr *auxhdr) | |
256 { | |
257 /* Update the checksum */ | |
258 hdr->checksum = calculate_checksum(hdr); | |
259 | |
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. */ | |
270 int | |
271 calculate_checksum(struct header *hdr) | |
272 { | |
273 int checksum, i, *ptr; | |
274 | |
275 checksum = 0; ptr = (int *) hdr; | |
276 | |
277 for (i=0; i<sizeof(*hdr)/sizeof(int)-1; i++) | |
278 checksum ^= ptr[i]; | |
279 | |
280 return(checksum); | |
281 } | |
282 | |
283 /* Copy size bytes from the old file to the new one. */ | |
284 void | |
3025 | 285 copy_file (int old, int new_, int size) |
428 | 286 { |
287 int len; | |
288 int buffer[8192]; /* word aligned will be faster */ | |
289 | |
290 for (; size > 0; size -= len) | |
291 { | |
442 | 292 len = size < sizeof (buffer) ? size : sizeof (buffer); |
293 if (read (old, buffer, len) != len) | |
294 { | |
295 perror ("Read failure on a.out file"); | |
296 exit (1); | |
297 } | |
3025 | 298 if (write (new_, buffer, len) != len) |
442 | 299 { |
300 perror ("Write failure in a.out file"); | |
301 exit (1); | |
302 } | |
428 | 303 } |
304 } | |
305 | |
306 /* Copy the rest of the file, up to EOF. */ | |
307 void | |
3025 | 308 copy_rest (int old, int new_) |
428 | 309 { |
310 int buffer[4096]; | |
311 int len; | |
312 | |
313 /* Copy bytes until end of file or error */ | |
314 while ( (len = read(old, buffer, sizeof(buffer))) > 0) | |
3025 | 315 if (write(new_, buffer, len) != len) break; |
428 | 316 |
317 if (len != 0) | |
318 { perror("Unable to copy the rest of the file"); exit(1); } | |
319 } | |
320 | |
321 #ifdef DEBUG | |
322 display_header(struct header *hdr, struct som_exec_auxhdr *auxhdr) | |
323 { | |
324 /* Display the header information (debug) */ | |
325 printf("\n\nFILE HEADER\n"); | |
326 printf("magic number %d \n", hdr->a_magic); | |
327 printf("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize); | |
328 printf("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize); | |
329 printf("entry %x \n", auxhdr->exec_entry); | |
330 printf("Bss segment size %u\n", auxhdr->exec_bsize); | |
331 printf("\n"); | |
332 printf("data file loc %d size %d\n", | |
333 auxhdr->exec_dfile, auxhdr->exec_dsize); | |
334 printf("som_length %d\n", hdr->som_length); | |
335 printf("unloadable sploc %d size %d\n", | |
336 hdr->unloadable_sp_location, hdr->unloadable_sp_size); | |
337 } | |
338 #endif /* DEBUG */ | |
339 | |
340 #ifdef HPUX_USE_SHLIBS | |
341 /* Added machinery for shared libs... see comments at the beginning of this file. */ | |
342 | |
343 void *Brk_On_Dump = 0; /* Brk value to restore... stored as a global */ | |
344 | |
442 | 345 static void |
346 Save_Shared_Data (void) | |
347 { | |
348 Brk_On_Dump = sbrk (0); | |
428 | 349 } |
350 | |
442 | 351 static void |
352 Restore_Shared_Data (void) | |
353 { | |
354 brk (Brk_On_Dump); | |
428 | 355 } |
356 | |
357 /* run_time_remap is the magic called by startup code in the dumped executable | |
442 | 358 if RUN_TIME_REMAP is set. */ |
359 int | |
2286 | 360 run_time_remap (char *UNUSED (dummy)) |
442 | 361 { |
362 Restore_Shared_Data (); | |
363 return 0; | |
364 } | |
428 | 365 #endif /* HPUX_USE_SHLIBS */ |