(define-ibuffer-sorter): Define the sorter to reverse sorting order if
[emacs.git] / src / unexhp9k800.c
blob96eb985f857e47ec96051016f68c70c82d9f11ad
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 #ifdef emacs
53 #include <config.h>
54 #endif
56 #include <stdio.h>
57 #include <fcntl.h>
58 #include <errno.h>
60 #include <a.out.h>
62 #ifdef HPUX_USE_SHLIBS
63 #include <dl.h>
64 #endif
66 /* brk value to restore, stored as a global.
67 This is really used only if we used shared libraries. */
68 static long brk_on_dump = 0;
70 /* Called from main, if we use shared libraries. */
71 int
72 run_time_remap (ignored)
73 char *ignored;
75 brk ((char *) brk_on_dump);
78 #undef roundup
79 #define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */
80 #define min(x,y) (((x) < (y)) ? (x) : (y))
83 /* Create a new a.out file, same as old but with current data space */
85 unexec (new_name, old_name, new_end_of_text, dummy1, dummy2)
86 char new_name[]; /* name of the new a.out file to be created */
87 char old_name[]; /* name of the old a.out file */
88 char *new_end_of_text; /* ptr to new edata/etext; NOT USED YET */
89 int dummy1, dummy2; /* not used by emacs */
91 int old, new;
92 int old_size, new_size;
93 struct header hdr;
94 struct som_exec_auxhdr auxhdr;
95 long i;
97 /* For the greatest flexibility, should create a temporary file in
98 the same directory as the new file. When everything is complete,
99 rename the temp file to the new name.
100 This way, a program could update its own a.out file even while
101 it is still executing. If problems occur, everything is still
102 intact. NOT implemented. */
104 /* Open the input and output a.out files */
105 old = open (old_name, O_RDONLY);
106 if (old < 0)
107 { perror (old_name); exit (1); }
108 new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
109 if (new < 0)
110 { perror (new_name); exit (1); }
112 /* Read the old headers */
113 read_header (old, &hdr, &auxhdr);
115 brk_on_dump = (long) sbrk (0);
117 /* Decide how large the new and old data areas are */
118 old_size = auxhdr.exec_dsize;
119 /* I suspect these two statements are separate
120 to avoid a compiler bug in hpux version 8. */
121 i = (long) sbrk (0);
122 new_size = i - auxhdr.exec_dmem;
124 /* Copy the old file to the new, up to the data space */
125 lseek (old, 0, 0);
126 copy_file (old, new, auxhdr.exec_dfile);
128 /* Skip the old data segment and write a new one */
129 lseek (old, old_size, 1);
130 save_data_space (new, &hdr, &auxhdr, new_size);
132 /* Copy the rest of the file */
133 copy_rest (old, new);
135 /* Update file pointers since we probably changed size of data area */
136 update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
138 /* Save the modified header */
139 write_header (new, &hdr, &auxhdr);
141 /* Close the binary file */
142 close (old);
143 close (new);
144 return 0;
147 /* Save current data space in the file, update header. */
149 save_data_space (file, hdr, auxhdr, size)
150 int file;
151 struct header *hdr;
152 struct som_exec_auxhdr *auxhdr;
153 int size;
155 /* Write the entire data space out to the file */
156 if (write (file, auxhdr->exec_dmem, size) != size)
157 { perror ("Can't save new data space"); exit (1); }
159 /* Update the header to reflect the new data size */
160 auxhdr->exec_dsize = size;
161 auxhdr->exec_bsize = 0;
164 /* Update the values of file pointers when something is inserted. */
166 update_file_ptrs (file, hdr, auxhdr, location, offset)
167 int file;
168 struct header *hdr;
169 struct som_exec_auxhdr *auxhdr;
170 unsigned int location;
171 int offset;
173 struct subspace_dictionary_record subspace;
174 int i;
176 /* Increase the overall size of the module */
177 hdr->som_length += offset;
179 /* Update the various file pointers in the header */
180 #define update(ptr) if (ptr > location) ptr = ptr + offset
181 update (hdr->aux_header_location);
182 update (hdr->space_strings_location);
183 update (hdr->init_array_location);
184 update (hdr->compiler_location);
185 update (hdr->symbol_location);
186 update (hdr->fixup_request_location);
187 update (hdr->symbol_strings_location);
188 update (hdr->unloadable_sp_location);
189 update (auxhdr->exec_tfile);
190 update (auxhdr->exec_dfile);
192 /* Do for each subspace dictionary entry */
193 lseek (file, hdr->subspace_location, 0);
194 for (i = 0; i < hdr->subspace_total; i++)
196 if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace))
197 { perror ("Can't read subspace record"); exit (1); }
199 /* If subspace has a file location, update it */
200 if (subspace.initialization_length > 0
201 && subspace.file_loc_init_value > location)
203 subspace.file_loc_init_value += offset;
204 lseek (file, -sizeof (subspace), 1);
205 if (write (file, &subspace, sizeof (subspace)) != sizeof (subspace))
206 { perror ("Can't update subspace record"); exit (1); }
210 /* Do for each initialization pointer record */
211 /* (I don't think it applies to executable files, only relocatables) */
212 #undef update
215 /* Read in the header records from an a.out file. */
217 read_header (file, hdr, auxhdr)
218 int file;
219 struct header *hdr;
220 struct som_exec_auxhdr *auxhdr;
223 /* Read the header in */
224 lseek (file, 0, 0);
225 if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
226 { perror ("Couldn't read header from a.out file"); exit (1); }
228 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
229 && hdr->a_magic != DEMAND_MAGIC)
231 fprintf (stderr, "a.out file doesn't have valid magic number\n");
232 exit (1);
235 lseek (file, hdr->aux_header_location, 0);
236 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
238 perror ("Couldn't read auxiliary header from a.out file");
239 exit (1);
243 /* Write out the header records into an a.out file. */
245 write_header (file, hdr, auxhdr)
246 int file;
247 struct header *hdr;
248 struct som_exec_auxhdr *auxhdr;
250 /* Update the checksum */
251 hdr->checksum = calculate_checksum (hdr);
253 /* Write the header back into the a.out file */
254 lseek (file, 0, 0);
255 if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
256 { perror ("Couldn't write header to a.out file"); exit (1); }
257 lseek (file, hdr->aux_header_location, 0);
258 if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
259 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
262 /* Calculate the checksum of a SOM header record. */
264 calculate_checksum (hdr)
265 struct header *hdr;
267 int checksum, i, *ptr;
269 checksum = 0; ptr = (int *) hdr;
271 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
272 checksum ^= ptr[i];
274 return (checksum);
277 /* Copy size bytes from the old file to the new one. */
279 copy_file (old, new, size)
280 int new, old;
281 int size;
283 int len;
284 int buffer[8192]; /* word aligned will be faster */
286 for (; size > 0; size -= len)
288 len = min (size, sizeof (buffer));
289 if (read (old, buffer, len) != len)
290 { perror ("Read failure on a.out file"); exit (1); }
291 if (write (new, buffer, len) != len)
292 { perror ("Write failure in a.out file"); exit (1); }
296 /* Copy the rest of the file, up to EOF. */
298 copy_rest (old, new)
299 int new, old;
301 int buffer[4096];
302 int len;
304 /* Copy bytes until end of file or error */
305 while ((len = read (old, buffer, sizeof (buffer))) > 0)
306 if (write (new, buffer, len) != len) break;
308 if (len != 0)
309 { perror ("Unable to copy the rest of the file"); exit (1); }
312 #ifdef DEBUG
313 display_header (hdr, auxhdr)
314 struct header *hdr;
315 struct som_exec_auxhdr *auxhdr;
317 /* Display the header information (debug) */
318 printf ("\n\nFILE HEADER\n");
319 printf ("magic number %d \n", hdr->a_magic);
320 printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
321 printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
322 printf ("entry %x \n", auxhdr->exec_entry);
323 printf ("Bss segment size %u\n", auxhdr->exec_bsize);
324 printf ("\n");
325 printf ("data file loc %d size %d\n",
326 auxhdr->exec_dfile, auxhdr->exec_dsize);
327 printf ("som_length %d\n", hdr->som_length);
328 printf ("unloadable sploc %d size %d\n",
329 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
331 #endif /* DEBUG */
333 /* arch-tag: d55a09ac-9427-4ec4-8496-cb9d7710774f
334 (do not change this comment) */