* insdel.c (count_size_as_multibyte): Check for string overflow.
[emacs.git] / src / unexhp9k800.c
blobf27415a252cf90e18a672940f4cd3742a7a75568
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 (ignored)
68 char *ignored;
70 brk ((char *) brk_on_dump);
73 #undef roundup
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 */
79 void
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 */
83 int old, new;
84 int old_size, new_size;
85 struct header hdr;
86 struct som_exec_auxhdr auxhdr;
87 long i;
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);
98 if (old < 0)
99 { perror (old_name); exit (1); }
100 new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
101 if (new < 0)
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. */
113 i = (long) sbrk (0);
114 new_size = i - auxhdr.exec_dmem;
116 /* Copy the old file to the new, up to the data space */
117 lseek (old, 0, 0);
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 */
134 close (old);
135 close (new);
138 /* Save current data space in the file, update header. */
140 save_data_space (file, hdr, auxhdr, size)
141 int file;
142 struct header *hdr;
143 struct som_exec_auxhdr *auxhdr;
144 int size;
146 /* Write the entire data space out to the file */
147 if (write (file, auxhdr->exec_dmem, size) != size)
148 { perror ("Can't save new data space"); exit (1); }
150 /* Update the header to reflect the new data size */
151 auxhdr->exec_dsize = size;
152 auxhdr->exec_bsize = 0;
155 /* Update the values of file pointers when something is inserted. */
157 update_file_ptrs (file, hdr, auxhdr, location, offset)
158 int file;
159 struct header *hdr;
160 struct som_exec_auxhdr *auxhdr;
161 unsigned int location;
162 int offset;
164 struct subspace_dictionary_record subspace;
165 int i;
167 /* Increase the overall size of the module */
168 hdr->som_length += offset;
170 /* Update the various file pointers in the header */
171 #define update(ptr) if (ptr > location) ptr = ptr + offset
172 update (hdr->aux_header_location);
173 update (hdr->space_strings_location);
174 update (hdr->init_array_location);
175 update (hdr->compiler_location);
176 update (hdr->symbol_location);
177 update (hdr->fixup_request_location);
178 update (hdr->symbol_strings_location);
179 update (hdr->unloadable_sp_location);
180 update (auxhdr->exec_tfile);
181 update (auxhdr->exec_dfile);
183 /* Do for each subspace dictionary entry */
184 lseek (file, hdr->subspace_location, 0);
185 for (i = 0; i < hdr->subspace_total; i++)
187 if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace))
188 { perror ("Can't read subspace record"); exit (1); }
190 /* If subspace has a file location, update it */
191 if (subspace.initialization_length > 0
192 && subspace.file_loc_init_value > location)
194 subspace.file_loc_init_value += offset;
195 lseek (file, -sizeof (subspace), 1);
196 if (write (file, &subspace, sizeof (subspace)) != sizeof (subspace))
197 { perror ("Can't update subspace record"); exit (1); }
201 /* Do for each initialization pointer record */
202 /* (I don't think it applies to executable files, only relocatables) */
203 #undef update
206 /* Read in the header records from an a.out file. */
208 read_header (file, hdr, auxhdr)
209 int file;
210 struct header *hdr;
211 struct som_exec_auxhdr *auxhdr;
214 /* Read the header in */
215 lseek (file, 0, 0);
216 if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
217 { perror ("Couldn't read header from a.out file"); exit (1); }
219 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
220 && hdr->a_magic != DEMAND_MAGIC)
222 fprintf (stderr, "a.out file doesn't have valid magic number\n");
223 exit (1);
226 lseek (file, hdr->aux_header_location, 0);
227 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
229 perror ("Couldn't read auxiliary header from a.out file");
230 exit (1);
234 /* Write out the header records into an a.out file. */
236 write_header (file, hdr, auxhdr)
237 int file;
238 struct header *hdr;
239 struct som_exec_auxhdr *auxhdr;
241 /* Update the checksum */
242 hdr->checksum = calculate_checksum (hdr);
244 /* Write the header back into the a.out file */
245 lseek (file, 0, 0);
246 if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
247 { perror ("Couldn't write header to a.out file"); exit (1); }
248 lseek (file, hdr->aux_header_location, 0);
249 if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
250 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
253 /* Calculate the checksum of a SOM header record. */
255 calculate_checksum (hdr)
256 struct header *hdr;
258 int checksum, i, *ptr;
260 checksum = 0; ptr = (int *) hdr;
262 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
263 checksum ^= ptr[i];
265 return (checksum);
268 /* Copy size bytes from the old file to the new one. */
270 copy_file (old, new, size)
271 int new, old;
272 int size;
274 int len;
275 int buffer[8192]; /* word aligned will be faster */
277 for (; size > 0; size -= len)
279 len = min (size, sizeof (buffer));
280 if (read (old, buffer, len) != len)
281 { perror ("Read failure on a.out file"); exit (1); }
282 if (write (new, buffer, len) != len)
283 { perror ("Write failure in a.out file"); exit (1); }
287 /* Copy the rest of the file, up to EOF. */
289 copy_rest (old, new)
290 int new, old;
292 int buffer[4096];
293 int len;
295 /* Copy bytes until end of file or error */
296 while ((len = read (old, buffer, sizeof (buffer))) > 0)
297 if (write (new, buffer, len) != len) break;
299 if (len != 0)
300 { perror ("Unable to copy the rest of the file"); exit (1); }
303 #ifdef DEBUG
304 display_header (hdr, auxhdr)
305 struct header *hdr;
306 struct som_exec_auxhdr *auxhdr;
308 /* Display the header information (debug) */
309 printf ("\n\nFILE HEADER\n");
310 printf ("magic number %d \n", hdr->a_magic);
311 printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
312 printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
313 printf ("entry %x \n", auxhdr->exec_entry);
314 printf ("Bss segment size %u\n", auxhdr->exec_bsize);
315 printf ("\n");
316 printf ("data file loc %d size %d\n",
317 auxhdr->exec_dfile, auxhdr->exec_dsize);
318 printf ("som_length %d\n", hdr->som_length);
319 printf ("unloadable sploc %d size %d\n",
320 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
322 #endif /* DEBUG */