* configure.in: Fix last change.
[emacs.git] / src / unexhp9k800.c
blobb9edd4f3a849d7d62151f55f7ed543aa9e75558a
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 <stdio.h>
54 #include <fcntl.h>
55 #include <errno.h>
56 #include <a.out.h>
57 #include <dl.h>
59 /* brk value to restore, stored as a global.
60 This is really used only if we used shared libraries. */
61 static long brk_on_dump = 0;
63 /* Called from main, if we use shared libraries. */
64 int
65 run_time_remap (ignored)
66 char *ignored;
68 brk ((char *) brk_on_dump);
71 #undef roundup
72 #define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */
73 #define min(x,y) (((x) < (y)) ? (x) : (y))
76 /* Create a new a.out file, same as old but with current data space */
77 int
78 unexec (const char *new_name, /* name of the new a.out file to be created */
79 const char *old_name) /* name of the old a.out file */
81 int old, new;
82 int old_size, new_size;
83 struct header hdr;
84 struct som_exec_auxhdr auxhdr;
85 long i;
87 /* For the greatest flexibility, should create a temporary file in
88 the same directory as the new file. When everything is complete,
89 rename the temp file to the new name.
90 This way, a program could update its own a.out file even while
91 it is still executing. If problems occur, everything is still
92 intact. NOT implemented. */
94 /* Open the input and output a.out files */
95 old = open (old_name, O_RDONLY);
96 if (old < 0)
97 { perror (old_name); exit (1); }
98 new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
99 if (new < 0)
100 { perror (new_name); exit (1); }
102 /* Read the old headers */
103 read_header (old, &hdr, &auxhdr);
105 brk_on_dump = (long) sbrk (0);
107 /* Decide how large the new and old data areas are */
108 old_size = auxhdr.exec_dsize;
109 /* I suspect these two statements are separate
110 to avoid a compiler bug in hpux version 8. */
111 i = (long) sbrk (0);
112 new_size = i - auxhdr.exec_dmem;
114 /* Copy the old file to the new, up to the data space */
115 lseek (old, 0, 0);
116 copy_file (old, new, auxhdr.exec_dfile);
118 /* Skip the old data segment and write a new one */
119 lseek (old, old_size, 1);
120 save_data_space (new, &hdr, &auxhdr, new_size);
122 /* Copy the rest of the file */
123 copy_rest (old, new);
125 /* Update file pointers since we probably changed size of data area */
126 update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
128 /* Save the modified header */
129 write_header (new, &hdr, &auxhdr);
131 /* Close the binary file */
132 close (old);
133 close (new);
134 return 0;
137 /* Save current data space in the file, update header. */
139 save_data_space (file, hdr, auxhdr, size)
140 int file;
141 struct header *hdr;
142 struct som_exec_auxhdr *auxhdr;
143 int size;
145 /* Write the entire data space out to the file */
146 if (write (file, auxhdr->exec_dmem, size) != size)
147 { perror ("Can't save new data space"); exit (1); }
149 /* Update the header to reflect the new data size */
150 auxhdr->exec_dsize = size;
151 auxhdr->exec_bsize = 0;
154 /* Update the values of file pointers when something is inserted. */
156 update_file_ptrs (file, hdr, auxhdr, location, offset)
157 int file;
158 struct header *hdr;
159 struct som_exec_auxhdr *auxhdr;
160 unsigned int location;
161 int offset;
163 struct subspace_dictionary_record subspace;
164 int i;
166 /* Increase the overall size of the module */
167 hdr->som_length += offset;
169 /* Update the various file pointers in the header */
170 #define update(ptr) if (ptr > location) ptr = ptr + offset
171 update (hdr->aux_header_location);
172 update (hdr->space_strings_location);
173 update (hdr->init_array_location);
174 update (hdr->compiler_location);
175 update (hdr->symbol_location);
176 update (hdr->fixup_request_location);
177 update (hdr->symbol_strings_location);
178 update (hdr->unloadable_sp_location);
179 update (auxhdr->exec_tfile);
180 update (auxhdr->exec_dfile);
182 /* Do for each subspace dictionary entry */
183 lseek (file, hdr->subspace_location, 0);
184 for (i = 0; i < hdr->subspace_total; i++)
186 if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace))
187 { perror ("Can't read subspace record"); exit (1); }
189 /* If subspace has a file location, update it */
190 if (subspace.initialization_length > 0
191 && subspace.file_loc_init_value > location)
193 subspace.file_loc_init_value += offset;
194 lseek (file, -sizeof (subspace), 1);
195 if (write (file, &subspace, sizeof (subspace)) != sizeof (subspace))
196 { perror ("Can't update subspace record"); exit (1); }
200 /* Do for each initialization pointer record */
201 /* (I don't think it applies to executable files, only relocatables) */
202 #undef update
205 /* Read in the header records from an a.out file. */
207 read_header (file, hdr, auxhdr)
208 int file;
209 struct header *hdr;
210 struct som_exec_auxhdr *auxhdr;
213 /* Read the header in */
214 lseek (file, 0, 0);
215 if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
216 { perror ("Couldn't read header from a.out file"); exit (1); }
218 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
219 && hdr->a_magic != DEMAND_MAGIC)
221 fprintf (stderr, "a.out file doesn't have valid magic number\n");
222 exit (1);
225 lseek (file, hdr->aux_header_location, 0);
226 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
228 perror ("Couldn't read auxiliary header from a.out file");
229 exit (1);
233 /* Write out the header records into an a.out file. */
235 write_header (file, hdr, auxhdr)
236 int file;
237 struct header *hdr;
238 struct som_exec_auxhdr *auxhdr;
240 /* Update the checksum */
241 hdr->checksum = calculate_checksum (hdr);
243 /* Write the header back into the a.out file */
244 lseek (file, 0, 0);
245 if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
246 { perror ("Couldn't write header to a.out file"); exit (1); }
247 lseek (file, hdr->aux_header_location, 0);
248 if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
249 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
252 /* Calculate the checksum of a SOM header record. */
254 calculate_checksum (hdr)
255 struct header *hdr;
257 int checksum, i, *ptr;
259 checksum = 0; ptr = (int *) hdr;
261 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
262 checksum ^= ptr[i];
264 return (checksum);
267 /* Copy size bytes from the old file to the new one. */
269 copy_file (old, new, size)
270 int new, old;
271 int size;
273 int len;
274 int buffer[8192]; /* word aligned will be faster */
276 for (; size > 0; size -= len)
278 len = min (size, sizeof (buffer));
279 if (read (old, buffer, len) != len)
280 { perror ("Read failure on a.out file"); exit (1); }
281 if (write (new, buffer, len) != len)
282 { perror ("Write failure in a.out file"); exit (1); }
286 /* Copy the rest of the file, up to EOF. */
288 copy_rest (old, new)
289 int new, old;
291 int buffer[4096];
292 int len;
294 /* Copy bytes until end of file or error */
295 while ((len = read (old, buffer, sizeof (buffer))) > 0)
296 if (write (new, buffer, len) != len) break;
298 if (len != 0)
299 { perror ("Unable to copy the rest of the file"); exit (1); }
302 #ifdef DEBUG
303 display_header (hdr, auxhdr)
304 struct header *hdr;
305 struct som_exec_auxhdr *auxhdr;
307 /* Display the header information (debug) */
308 printf ("\n\nFILE HEADER\n");
309 printf ("magic number %d \n", hdr->a_magic);
310 printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
311 printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
312 printf ("entry %x \n", auxhdr->exec_entry);
313 printf ("Bss segment size %u\n", auxhdr->exec_bsize);
314 printf ("\n");
315 printf ("data file loc %d size %d\n",
316 auxhdr->exec_dfile, auxhdr->exec_dsize);
317 printf ("som_length %d\n", hdr->som_length);
318 printf ("unloadable sploc %d size %d\n",
319 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
321 #endif /* DEBUG */
323 /* arch-tag: d55a09ac-9427-4ec4-8496-cb9d7710774f
324 (do not change this comment) */