ChangeLog fixes from M-x authors
[emacs.git] / src / unexhp9k800.c
blobce65faffd4e59d73b3f9e88b90bc945375efaaa9
1 /* Unexec for HP 9000 Series 800 machines.
3 This file is in the public domain.
5 Author: John V. Morris
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
21 might break it.
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.
36 CAVEATS:
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.
47 void _sigreturn ();
48 ...
49 sigsetreturn (_sigreturn);
52 #include <config.h>
53 #include "unexec.h"
55 #include <stdio.h>
56 #include <fcntl.h>
57 #include <errno.h>
58 #include <a.out.h>
59 #include <dl.h>
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. */
66 int
67 run_time_remap (char *ignored)
69 brk ((char *) brk_on_dump);
72 #undef roundup
73 #define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */
74 #define min(x,y) (((x) < (y)) ? (x) : (y))
76 /* Save current data space in the file, update header. */
78 static void
79 save_data_space (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
80 int size)
82 /* Write the entire data space out to the file */
83 if (write (file, auxhdr->exec_dmem, size) != size)
84 { perror ("Can't save new data space"); exit (1); }
86 /* Update the header to reflect the new data size */
87 auxhdr->exec_dsize = size;
88 auxhdr->exec_bsize = 0;
91 /* Update the values of file pointers when something is inserted. */
93 static void
94 update_file_ptrs (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
95 unsigned int location, int offset)
97 struct subspace_dictionary_record subspace;
98 int i;
100 /* Increase the overall size of the module */
101 hdr->som_length += offset;
103 /* Update the various file pointers in the header */
104 #define update(ptr) if (ptr > location) ptr = ptr + offset
105 update (hdr->aux_header_location);
106 update (hdr->space_strings_location);
107 update (hdr->init_array_location);
108 update (hdr->compiler_location);
109 update (hdr->symbol_location);
110 update (hdr->fixup_request_location);
111 update (hdr->symbol_strings_location);
112 update (hdr->unloadable_sp_location);
113 update (auxhdr->exec_tfile);
114 update (auxhdr->exec_dfile);
116 /* Do for each subspace dictionary entry */
117 lseek (file, hdr->subspace_location, 0);
118 for (i = 0; i < hdr->subspace_total; i++)
120 if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace))
121 { perror ("Can't read subspace record"); exit (1); }
123 /* If subspace has a file location, update it */
124 if (subspace.initialization_length > 0
125 && subspace.file_loc_init_value > location)
127 subspace.file_loc_init_value += offset;
128 lseek (file, -sizeof (subspace), 1);
129 if (write (file, &subspace, sizeof (subspace)) != sizeof (subspace))
130 { perror ("Can't update subspace record"); exit (1); }
134 /* Do for each initialization pointer record */
135 /* (I don't think it applies to executable files, only relocatables) */
136 #undef update
139 /* Read in the header records from an a.out file. */
141 static void
142 read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
145 /* Read the header in */
146 lseek (file, 0, 0);
147 if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
148 { perror ("Couldn't read header from a.out file"); exit (1); }
150 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
151 && hdr->a_magic != DEMAND_MAGIC)
153 fprintf (stderr, "a.out file doesn't have valid magic number\n");
154 exit (1);
157 lseek (file, hdr->aux_header_location, 0);
158 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
160 perror ("Couldn't read auxiliary header from a.out file");
161 exit (1);
165 /* Write out the header records into an a.out file. */
167 static void
168 write_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
170 /* Update the checksum */
171 hdr->checksum = calculate_checksum (hdr);
173 /* Write the header back into the a.out file */
174 lseek (file, 0, 0);
175 if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
176 { perror ("Couldn't write header to a.out file"); exit (1); }
177 lseek (file, hdr->aux_header_location, 0);
178 if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
179 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
182 /* Calculate the checksum of a SOM header record. */
184 static int
185 calculate_checksum (struct header *hdr)
187 int checksum, i, *ptr;
189 checksum = 0; ptr = (int *) hdr;
191 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
192 checksum ^= ptr[i];
194 return (checksum);
197 /* Copy size bytes from the old file to the new one. */
199 static void
200 copy_file (int old, int new, int size)
202 int len;
203 int buffer[8192]; /* word aligned will be faster */
205 for (; size > 0; size -= len)
207 len = min (size, sizeof (buffer));
208 if (read (old, buffer, len) != len)
209 { perror ("Read failure on a.out file"); exit (1); }
210 if (write (new, buffer, len) != len)
211 { perror ("Write failure in a.out file"); exit (1); }
215 /* Copy the rest of the file, up to EOF. */
217 static void
218 copy_rest (int old, int new)
220 int buffer[4096];
221 int len;
223 /* Copy bytes until end of file or error */
224 while ((len = read (old, buffer, sizeof (buffer))) > 0)
225 if (write (new, buffer, len) != len) break;
227 if (len != 0)
228 { perror ("Unable to copy the rest of the file"); exit (1); }
231 #ifdef DEBUG
232 static void
233 display_header (struct header *hdr, struct som_exec_auxhdr *auxhdr)
235 /* Display the header information (debug) */
236 printf ("\n\nFILE HEADER\n");
237 printf ("magic number %d \n", hdr->a_magic);
238 printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
239 printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
240 printf ("entry %x \n", auxhdr->exec_entry);
241 printf ("Bss segment size %u\n", auxhdr->exec_bsize);
242 printf ("\n");
243 printf ("data file loc %d size %d\n",
244 auxhdr->exec_dfile, auxhdr->exec_dsize);
245 printf ("som_length %d\n", hdr->som_length);
246 printf ("unloadable sploc %d size %d\n",
247 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
249 #endif /* DEBUG */
252 /* Create a new a.out file, same as old but with current data space */
253 void
254 unexec (const char *new_name, /* name of the new a.out file to be created */
255 const char *old_name) /* name of the old a.out file */
257 int old, new;
258 int old_size, new_size;
259 struct header hdr;
260 struct som_exec_auxhdr auxhdr;
261 long i;
263 /* For the greatest flexibility, should create a temporary file in
264 the same directory as the new file. When everything is complete,
265 rename the temp file to the new name.
266 This way, a program could update its own a.out file even while
267 it is still executing. If problems occur, everything is still
268 intact. NOT implemented. */
270 /* Open the input and output a.out files */
271 old = open (old_name, O_RDONLY);
272 if (old < 0)
273 { perror (old_name); exit (1); }
274 new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
275 if (new < 0)
276 { perror (new_name); exit (1); }
278 /* Read the old headers */
279 read_header (old, &hdr, &auxhdr);
281 brk_on_dump = (long) sbrk (0);
283 /* Decide how large the new and old data areas are */
284 old_size = auxhdr.exec_dsize;
285 /* I suspect these two statements are separate
286 to avoid a compiler bug in hpux version 8. */
287 i = (long) sbrk (0);
288 new_size = i - auxhdr.exec_dmem;
290 /* Copy the old file to the new, up to the data space */
291 lseek (old, 0, 0);
292 copy_file (old, new, auxhdr.exec_dfile);
294 /* Skip the old data segment and write a new one */
295 lseek (old, old_size, 1);
296 save_data_space (new, &hdr, &auxhdr, new_size);
298 /* Copy the rest of the file */
299 copy_rest (old, new);
301 /* Update file pointers since we probably changed size of data area */
302 update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
304 /* Save the modified header */
305 write_header (new, &hdr, &auxhdr);
307 /* Close the binary file */
308 close (old);
309 close (new);