1 /* Unexec for HP 9000 Series 800 machines.
3 This file is in the public domain.
7 This file was written by John V. Morris at Hewlett Packard.
8 Both the author and Hewlett Packard Co. have disclaimed the
9 copyright on this file, and it is therefore in the public domain.
10 (Search for "hp9k800" in copyright.list.)
14 Bob Desinger <hpsemc!bd@hplabs.hp.com>
16 Note that the GNU project considers support for HP operation a
17 peripheral activity which should not be allowed to divert effort
18 from development of the GNU system. Changes in this code will be
19 installed when users send them in, but aside from that we don't
20 plan to think about it, or about whether other Emacs maintenance
24 Unexec creates a copy of the old a.out file, and replaces the old data
25 area with the current data area. When the new file is executed, the
26 process will see the same data structures and data values that the
27 original process had when unexec was called.
29 Unlike other versions of unexec, this one copies symbol table and
30 debug information to the new a.out file. Thus, the new a.out file
31 may be debugged with symbolic debuggers.
33 If you fix any bugs in this, I'd like to incorporate your fixes.
34 Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
37 This routine saves the current value of all static and external
38 variables. This means that any data structure that needs to be
39 initialized must be explicitly reset. Variables will not have their
40 expected default values.
42 Unfortunately, the HP-UX signal handler has internal initialization
43 flags which are not explicitly reset. Thus, for signals to work in
44 conjunction with this routine, the following code must executed when
45 the new process starts up.
49 sigsetreturn (_sigreturn);
64 /* brk value to restore, stored as a global.
65 This is really used only if we used shared libraries. */
66 static long brk_on_dump
= 0;
68 /* Called from main, if we use shared libraries. */
70 run_time_remap (ignored
)
73 brk ((char *) brk_on_dump
);
77 #define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */
78 #define min(x,y) (((x) < (y)) ? (x) : (y))
81 /* Create a new a.out file, same as old but with current data space */
83 unexec (const char *new_name
, /* name of the new a.out file to be created */
84 const char *old_name
) /* name of the old a.out file */
87 int old_size
, new_size
;
89 struct som_exec_auxhdr auxhdr
;
92 /* For the greatest flexibility, should create a temporary file in
93 the same directory as the new file. When everything is complete,
94 rename the temp file to the new name.
95 This way, a program could update its own a.out file even while
96 it is still executing. If problems occur, everything is still
97 intact. NOT implemented. */
99 /* Open the input and output a.out files */
100 old
= open (old_name
, O_RDONLY
);
102 { perror (old_name
); exit (1); }
103 new = open (new_name
, O_CREAT
|O_RDWR
|O_TRUNC
, 0777);
105 { perror (new_name
); exit (1); }
107 /* Read the old headers */
108 read_header (old
, &hdr
, &auxhdr
);
110 brk_on_dump
= (long) sbrk (0);
112 /* Decide how large the new and old data areas are */
113 old_size
= auxhdr
.exec_dsize
;
114 /* I suspect these two statements are separate
115 to avoid a compiler bug in hpux version 8. */
117 new_size
= i
- auxhdr
.exec_dmem
;
119 /* Copy the old file to the new, up to the data space */
121 copy_file (old
, new, auxhdr
.exec_dfile
);
123 /* Skip the old data segment and write a new one */
124 lseek (old
, old_size
, 1);
125 save_data_space (new, &hdr
, &auxhdr
, new_size
);
127 /* Copy the rest of the file */
128 copy_rest (old
, new);
130 /* Update file pointers since we probably changed size of data area */
131 update_file_ptrs (new, &hdr
, &auxhdr
, auxhdr
.exec_dfile
, new_size
-old_size
);
133 /* Save the modified header */
134 write_header (new, &hdr
, &auxhdr
);
136 /* Close the binary file */
142 /* Save current data space in the file, update header. */
144 save_data_space (file
, hdr
, auxhdr
, size
)
147 struct som_exec_auxhdr
*auxhdr
;
150 /* Write the entire data space out to the file */
151 if (write (file
, auxhdr
->exec_dmem
, size
) != size
)
152 { perror ("Can't save new data space"); exit (1); }
154 /* Update the header to reflect the new data size */
155 auxhdr
->exec_dsize
= size
;
156 auxhdr
->exec_bsize
= 0;
159 /* Update the values of file pointers when something is inserted. */
161 update_file_ptrs (file
, hdr
, auxhdr
, location
, offset
)
164 struct som_exec_auxhdr
*auxhdr
;
165 unsigned int location
;
168 struct subspace_dictionary_record subspace
;
171 /* Increase the overall size of the module */
172 hdr
->som_length
+= offset
;
174 /* Update the various file pointers in the header */
175 #define update(ptr) if (ptr > location) ptr = ptr + offset
176 update (hdr
->aux_header_location
);
177 update (hdr
->space_strings_location
);
178 update (hdr
->init_array_location
);
179 update (hdr
->compiler_location
);
180 update (hdr
->symbol_location
);
181 update (hdr
->fixup_request_location
);
182 update (hdr
->symbol_strings_location
);
183 update (hdr
->unloadable_sp_location
);
184 update (auxhdr
->exec_tfile
);
185 update (auxhdr
->exec_dfile
);
187 /* Do for each subspace dictionary entry */
188 lseek (file
, hdr
->subspace_location
, 0);
189 for (i
= 0; i
< hdr
->subspace_total
; i
++)
191 if (read (file
, &subspace
, sizeof (subspace
)) != sizeof (subspace
))
192 { perror ("Can't read subspace record"); exit (1); }
194 /* If subspace has a file location, update it */
195 if (subspace
.initialization_length
> 0
196 && subspace
.file_loc_init_value
> location
)
198 subspace
.file_loc_init_value
+= offset
;
199 lseek (file
, -sizeof (subspace
), 1);
200 if (write (file
, &subspace
, sizeof (subspace
)) != sizeof (subspace
))
201 { perror ("Can't update subspace record"); exit (1); }
205 /* Do for each initialization pointer record */
206 /* (I don't think it applies to executable files, only relocatables) */
210 /* Read in the header records from an a.out file. */
212 read_header (file
, hdr
, auxhdr
)
215 struct som_exec_auxhdr
*auxhdr
;
218 /* Read the header in */
220 if (read (file
, hdr
, sizeof (*hdr
)) != sizeof (*hdr
))
221 { perror ("Couldn't read header from a.out file"); exit (1); }
223 if (hdr
->a_magic
!= EXEC_MAGIC
&& hdr
->a_magic
!= SHARE_MAGIC
224 && hdr
->a_magic
!= DEMAND_MAGIC
)
226 fprintf (stderr
, "a.out file doesn't have valid magic number\n");
230 lseek (file
, hdr
->aux_header_location
, 0);
231 if (read (file
, auxhdr
, sizeof (*auxhdr
)) != sizeof (*auxhdr
))
233 perror ("Couldn't read auxiliary header from a.out file");
238 /* Write out the header records into an a.out file. */
240 write_header (file
, hdr
, auxhdr
)
243 struct som_exec_auxhdr
*auxhdr
;
245 /* Update the checksum */
246 hdr
->checksum
= calculate_checksum (hdr
);
248 /* Write the header back into the a.out file */
250 if (write (file
, hdr
, sizeof (*hdr
)) != sizeof (*hdr
))
251 { perror ("Couldn't write header to a.out file"); exit (1); }
252 lseek (file
, hdr
->aux_header_location
, 0);
253 if (write (file
, auxhdr
, sizeof (*auxhdr
)) != sizeof (*auxhdr
))
254 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
257 /* Calculate the checksum of a SOM header record. */
259 calculate_checksum (hdr
)
262 int checksum
, i
, *ptr
;
264 checksum
= 0; ptr
= (int *) hdr
;
266 for (i
= 0; i
< sizeof (*hdr
) / sizeof (int) - 1; i
++)
272 /* Copy size bytes from the old file to the new one. */
274 copy_file (old
, new, size
)
279 int buffer
[8192]; /* word aligned will be faster */
281 for (; size
> 0; size
-= len
)
283 len
= min (size
, sizeof (buffer
));
284 if (read (old
, buffer
, len
) != len
)
285 { perror ("Read failure on a.out file"); exit (1); }
286 if (write (new, buffer
, len
) != len
)
287 { perror ("Write failure in a.out file"); exit (1); }
291 /* Copy the rest of the file, up to EOF. */
299 /* Copy bytes until end of file or error */
300 while ((len
= read (old
, buffer
, sizeof (buffer
))) > 0)
301 if (write (new, buffer
, len
) != len
) break;
304 { perror ("Unable to copy the rest of the file"); exit (1); }
308 display_header (hdr
, auxhdr
)
310 struct som_exec_auxhdr
*auxhdr
;
312 /* Display the header information (debug) */
313 printf ("\n\nFILE HEADER\n");
314 printf ("magic number %d \n", hdr
->a_magic
);
315 printf ("text loc %.8x size %d \n", auxhdr
->exec_tmem
, auxhdr
->exec_tsize
);
316 printf ("data loc %.8x size %d \n", auxhdr
->exec_dmem
, auxhdr
->exec_dsize
);
317 printf ("entry %x \n", auxhdr
->exec_entry
);
318 printf ("Bss segment size %u\n", auxhdr
->exec_bsize
);
320 printf ("data file loc %d size %d\n",
321 auxhdr
->exec_dfile
, auxhdr
->exec_dsize
);
322 printf ("som_length %d\n", hdr
->som_length
);
323 printf ("unloadable sploc %d size %d\n",
324 hdr
->unloadable_sp_location
, hdr
->unloadable_sp_size
);
328 /* arch-tag: d55a09ac-9427-4ec4-8496-cb9d7710774f
329 (do not change this comment) */