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);
61 /* brk value to restore, stored as a global.
62 This is really used only if we used shared libraries. */
63 static long brk_on_dump
= 0;
65 /* Called from main, if we use shared libraries. */
67 run_time_remap (ignored
)
70 brk ((char *) brk_on_dump
);
74 #define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */
75 #define min(x,y) (((x) < (y)) ? (x) : (y))
78 /* Create a new a.out file, same as old but with current data space */
80 unexec (const char *new_name
, /* name of the new a.out file to be created */
81 const char *old_name
) /* name of the old a.out file */
84 int old_size
, new_size
;
86 struct som_exec_auxhdr auxhdr
;
89 /* For the greatest flexibility, should create a temporary file in
90 the same directory as the new file. When everything is complete,
91 rename the temp file to the new name.
92 This way, a program could update its own a.out file even while
93 it is still executing. If problems occur, everything is still
94 intact. NOT implemented. */
96 /* Open the input and output a.out files */
97 old
= open (old_name
, O_RDONLY
);
99 { perror (old_name
); exit (1); }
100 new = open (new_name
, O_CREAT
|O_RDWR
|O_TRUNC
, 0777);
102 { perror (new_name
); exit (1); }
104 /* Read the old headers */
105 read_header (old
, &hdr
, &auxhdr
);
107 brk_on_dump
= (long) sbrk (0);
109 /* Decide how large the new and old data areas are */
110 old_size
= auxhdr
.exec_dsize
;
111 /* I suspect these two statements are separate
112 to avoid a compiler bug in hpux version 8. */
114 new_size
= i
- auxhdr
.exec_dmem
;
116 /* Copy the old file to the new, up to the data space */
118 copy_file (old
, new, auxhdr
.exec_dfile
);
120 /* Skip the old data segment and write a new one */
121 lseek (old
, old_size
, 1);
122 save_data_space (new, &hdr
, &auxhdr
, new_size
);
124 /* Copy the rest of the file */
125 copy_rest (old
, new);
127 /* Update file pointers since we probably changed size of data area */
128 update_file_ptrs (new, &hdr
, &auxhdr
, auxhdr
.exec_dfile
, new_size
-old_size
);
130 /* Save the modified header */
131 write_header (new, &hdr
, &auxhdr
);
133 /* Close the binary file */
139 /* Save current data space in the file, update header. */
141 save_data_space (file
, hdr
, auxhdr
, size
)
144 struct som_exec_auxhdr
*auxhdr
;
147 /* Write the entire data space out to the file */
148 if (write (file
, auxhdr
->exec_dmem
, size
) != size
)
149 { perror ("Can't save new data space"); exit (1); }
151 /* Update the header to reflect the new data size */
152 auxhdr
->exec_dsize
= size
;
153 auxhdr
->exec_bsize
= 0;
156 /* Update the values of file pointers when something is inserted. */
158 update_file_ptrs (file
, hdr
, auxhdr
, location
, offset
)
161 struct som_exec_auxhdr
*auxhdr
;
162 unsigned int location
;
165 struct subspace_dictionary_record subspace
;
168 /* Increase the overall size of the module */
169 hdr
->som_length
+= offset
;
171 /* Update the various file pointers in the header */
172 #define update(ptr) if (ptr > location) ptr = ptr + offset
173 update (hdr
->aux_header_location
);
174 update (hdr
->space_strings_location
);
175 update (hdr
->init_array_location
);
176 update (hdr
->compiler_location
);
177 update (hdr
->symbol_location
);
178 update (hdr
->fixup_request_location
);
179 update (hdr
->symbol_strings_location
);
180 update (hdr
->unloadable_sp_location
);
181 update (auxhdr
->exec_tfile
);
182 update (auxhdr
->exec_dfile
);
184 /* Do for each subspace dictionary entry */
185 lseek (file
, hdr
->subspace_location
, 0);
186 for (i
= 0; i
< hdr
->subspace_total
; i
++)
188 if (read (file
, &subspace
, sizeof (subspace
)) != sizeof (subspace
))
189 { perror ("Can't read subspace record"); exit (1); }
191 /* If subspace has a file location, update it */
192 if (subspace
.initialization_length
> 0
193 && subspace
.file_loc_init_value
> location
)
195 subspace
.file_loc_init_value
+= offset
;
196 lseek (file
, -sizeof (subspace
), 1);
197 if (write (file
, &subspace
, sizeof (subspace
)) != sizeof (subspace
))
198 { perror ("Can't update subspace record"); exit (1); }
202 /* Do for each initialization pointer record */
203 /* (I don't think it applies to executable files, only relocatables) */
207 /* Read in the header records from an a.out file. */
209 read_header (file
, hdr
, auxhdr
)
212 struct som_exec_auxhdr
*auxhdr
;
215 /* Read the header in */
217 if (read (file
, hdr
, sizeof (*hdr
)) != sizeof (*hdr
))
218 { perror ("Couldn't read header from a.out file"); exit (1); }
220 if (hdr
->a_magic
!= EXEC_MAGIC
&& hdr
->a_magic
!= SHARE_MAGIC
221 && hdr
->a_magic
!= DEMAND_MAGIC
)
223 fprintf (stderr
, "a.out file doesn't have valid magic number\n");
227 lseek (file
, hdr
->aux_header_location
, 0);
228 if (read (file
, auxhdr
, sizeof (*auxhdr
)) != sizeof (*auxhdr
))
230 perror ("Couldn't read auxiliary header from a.out file");
235 /* Write out the header records into an a.out file. */
237 write_header (file
, hdr
, auxhdr
)
240 struct som_exec_auxhdr
*auxhdr
;
242 /* Update the checksum */
243 hdr
->checksum
= calculate_checksum (hdr
);
245 /* Write the header back into the a.out file */
247 if (write (file
, hdr
, sizeof (*hdr
)) != sizeof (*hdr
))
248 { perror ("Couldn't write header to a.out file"); exit (1); }
249 lseek (file
, hdr
->aux_header_location
, 0);
250 if (write (file
, auxhdr
, sizeof (*auxhdr
)) != sizeof (*auxhdr
))
251 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
254 /* Calculate the checksum of a SOM header record. */
256 calculate_checksum (hdr
)
259 int checksum
, i
, *ptr
;
261 checksum
= 0; ptr
= (int *) hdr
;
263 for (i
= 0; i
< sizeof (*hdr
) / sizeof (int) - 1; i
++)
269 /* Copy size bytes from the old file to the new one. */
271 copy_file (old
, new, size
)
276 int buffer
[8192]; /* word aligned will be faster */
278 for (; size
> 0; size
-= len
)
280 len
= min (size
, sizeof (buffer
));
281 if (read (old
, buffer
, len
) != len
)
282 { perror ("Read failure on a.out file"); exit (1); }
283 if (write (new, buffer
, len
) != len
)
284 { perror ("Write failure in a.out file"); exit (1); }
288 /* Copy the rest of the file, up to EOF. */
296 /* Copy bytes until end of file or error */
297 while ((len
= read (old
, buffer
, sizeof (buffer
))) > 0)
298 if (write (new, buffer
, len
) != len
) break;
301 { perror ("Unable to copy the rest of the file"); exit (1); }
305 display_header (hdr
, auxhdr
)
307 struct som_exec_auxhdr
*auxhdr
;
309 /* Display the header information (debug) */
310 printf ("\n\nFILE HEADER\n");
311 printf ("magic number %d \n", hdr
->a_magic
);
312 printf ("text loc %.8x size %d \n", auxhdr
->exec_tmem
, auxhdr
->exec_tsize
);
313 printf ("data loc %.8x size %d \n", auxhdr
->exec_dmem
, auxhdr
->exec_dsize
);
314 printf ("entry %x \n", auxhdr
->exec_entry
);
315 printf ("Bss segment size %u\n", auxhdr
->exec_bsize
);
317 printf ("data file loc %d size %d\n",
318 auxhdr
->exec_dfile
, auxhdr
->exec_dsize
);
319 printf ("som_length %d\n", hdr
->som_length
);
320 printf ("unloadable sploc %d size %d\n",
321 hdr
->unloadable_sp_location
, hdr
->unloadable_sp_size
);