lisp/emacs-lisp/cl-macs.el: Fix typos in docstrings.
[bpt/emacs.git] / src / unexhp9k800.c
CommitLineData
89117df0
GM
1/* Unexec for HP 9000 Series 800 machines.
2
3 This file is in the public domain.
4
5 Author: John V. Morris
6
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.)
11*/
12
13/*
14 Bob Desinger <hpsemc!bd@hplabs.hp.com>
15
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.
22
23
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.
28
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.
32
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.
35
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.
41
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.
46
47 void _sigreturn ();
48 ...
49 sigsetreturn (_sigreturn);
50*/
51\f
89117df0 52#include <config.h>
ce701a33 53#include "unexec.h"
406af475 54#include "lisp.h"
ce701a33 55
89117df0
GM
56#include <stdio.h>
57#include <fcntl.h>
58#include <errno.h>
89117df0 59#include <a.out.h>
89117df0 60#include <dl.h>
89117df0
GM
61
62/* brk value to restore, stored as a global.
63 This is really used only if we used shared libraries. */
64static long brk_on_dump = 0;
65
66/* Called from main, if we use shared libraries. */
67int
1dae0f0a 68run_time_remap (char *ignored)
89117df0
GM
69{
70 brk ((char *) brk_on_dump);
71}
72
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))
76
89117df0
GM
77/* Save current data space in the file, update header. */
78
1dae0f0a
AS
79static void
80save_data_space (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
81 int size)
89117df0
GM
82{
83 /* Write the entire data space out to the file */
84 if (write (file, auxhdr->exec_dmem, size) != size)
85 { perror ("Can't save new data space"); exit (1); }
86
87 /* Update the header to reflect the new data size */
88 auxhdr->exec_dsize = size;
89 auxhdr->exec_bsize = 0;
90}
91
92/* Update the values of file pointers when something is inserted. */
93
1dae0f0a
AS
94static void
95update_file_ptrs (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
96 unsigned int location, int offset)
89117df0
GM
97{
98 struct subspace_dictionary_record subspace;
99 int i;
100
101 /* Increase the overall size of the module */
102 hdr->som_length += offset;
103
104 /* Update the various file pointers in the header */
105#define update(ptr) if (ptr > location) ptr = ptr + offset
106 update (hdr->aux_header_location);
107 update (hdr->space_strings_location);
108 update (hdr->init_array_location);
109 update (hdr->compiler_location);
110 update (hdr->symbol_location);
111 update (hdr->fixup_request_location);
112 update (hdr->symbol_strings_location);
113 update (hdr->unloadable_sp_location);
114 update (auxhdr->exec_tfile);
115 update (auxhdr->exec_dfile);
116
117 /* Do for each subspace dictionary entry */
118 lseek (file, hdr->subspace_location, 0);
119 for (i = 0; i < hdr->subspace_total; i++)
120 {
121 if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace))
122 { perror ("Can't read subspace record"); exit (1); }
123
124 /* If subspace has a file location, update it */
125 if (subspace.initialization_length > 0
126 && subspace.file_loc_init_value > location)
127 {
128 subspace.file_loc_init_value += offset;
129 lseek (file, -sizeof (subspace), 1);
130 if (write (file, &subspace, sizeof (subspace)) != sizeof (subspace))
131 { perror ("Can't update subspace record"); exit (1); }
132 }
133 }
134
135 /* Do for each initialization pointer record */
136 /* (I don't think it applies to executable files, only relocatables) */
137#undef update
138}
139
140/* Read in the header records from an a.out file. */
141
1dae0f0a
AS
142static void
143read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
144{
145
146 /* Read the header in */
147 lseek (file, 0, 0);
148 if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
149 { perror ("Couldn't read header from a.out file"); exit (1); }
150
151 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
152 && hdr->a_magic != DEMAND_MAGIC)
153 {
5af1f9fe 154 fprintf (stderr, "a.out file doesn't have valid magic number\n");
89117df0
GM
155 exit (1);
156 }
157
158 lseek (file, hdr->aux_header_location, 0);
159 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
160 {
161 perror ("Couldn't read auxiliary header from a.out file");
162 exit (1);
163 }
164}
165
166/* Write out the header records into an a.out file. */
167
1dae0f0a
AS
168static void
169write_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
170{
171 /* Update the checksum */
172 hdr->checksum = calculate_checksum (hdr);
173
174 /* Write the header back into the a.out file */
175 lseek (file, 0, 0);
176 if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
177 { perror ("Couldn't write header to a.out file"); exit (1); }
178 lseek (file, hdr->aux_header_location, 0);
179 if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
180 { perror ("Couldn't write auxiliary header to a.out file"); exit (1); }
181}
182
183/* Calculate the checksum of a SOM header record. */
184
1dae0f0a
AS
185static int
186calculate_checksum (struct header *hdr)
89117df0
GM
187{
188 int checksum, i, *ptr;
189
190 checksum = 0; ptr = (int *) hdr;
191
192 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
193 checksum ^= ptr[i];
194
195 return (checksum);
196}
197
198/* Copy size bytes from the old file to the new one. */
199
1dae0f0a
AS
200static void
201copy_file (int old, int new, int size)
89117df0
GM
202{
203 int len;
204 int buffer[8192]; /* word aligned will be faster */
205
206 for (; size > 0; size -= len)
207 {
208 len = min (size, sizeof (buffer));
209 if (read (old, buffer, len) != len)
210 { perror ("Read failure on a.out file"); exit (1); }
211 if (write (new, buffer, len) != len)
212 { perror ("Write failure in a.out file"); exit (1); }
213 }
214}
215
216/* Copy the rest of the file, up to EOF. */
217
1dae0f0a
AS
218static void
219copy_rest (int old, int new)
89117df0
GM
220{
221 int buffer[4096];
222 int len;
223
224 /* Copy bytes until end of file or error */
225 while ((len = read (old, buffer, sizeof (buffer))) > 0)
226 if (write (new, buffer, len) != len) break;
227
228 if (len != 0)
229 { perror ("Unable to copy the rest of the file"); exit (1); }
230}
231
232#ifdef DEBUG
1dae0f0a
AS
233static void
234display_header (struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
235{
236 /* Display the header information (debug) */
237 printf ("\n\nFILE HEADER\n");
238 printf ("magic number %d \n", hdr->a_magic);
239 printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
240 printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
241 printf ("entry %x \n", auxhdr->exec_entry);
242 printf ("Bss segment size %u\n", auxhdr->exec_bsize);
243 printf ("\n");
244 printf ("data file loc %d size %d\n",
245 auxhdr->exec_dfile, auxhdr->exec_dsize);
246 printf ("som_length %d\n", hdr->som_length);
247 printf ("unloadable sploc %d size %d\n",
248 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
249}
250#endif /* DEBUG */
1dae0f0a
AS
251
252
253/* Create a new a.out file, same as old but with current data space */
254void
255unexec (const char *new_name, /* name of the new a.out file to be created */
256 const char *old_name) /* name of the old a.out file */
257{
258 int old, new;
259 int old_size, new_size;
260 struct header hdr;
261 struct som_exec_auxhdr auxhdr;
262 long i;
263
264 /* For the greatest flexibility, should create a temporary file in
265 the same directory as the new file. When everything is complete,
266 rename the temp file to the new name.
267 This way, a program could update its own a.out file even while
268 it is still executing. If problems occur, everything is still
269 intact. NOT implemented. */
270
271 /* Open the input and output a.out files */
406af475 272 old = emacs_open (old_name, O_RDONLY, 0);
1dae0f0a
AS
273 if (old < 0)
274 { perror (old_name); exit (1); }
406af475 275 new = emacs_open (new_name, O_CREAT | O_RDWR | O_TRUNC, 0777);
1dae0f0a
AS
276 if (new < 0)
277 { perror (new_name); exit (1); }
278
279 /* Read the old headers */
280 read_header (old, &hdr, &auxhdr);
281
282 brk_on_dump = (long) sbrk (0);
283
284 /* Decide how large the new and old data areas are */
285 old_size = auxhdr.exec_dsize;
286 /* I suspect these two statements are separate
287 to avoid a compiler bug in hpux version 8. */
288 i = (long) sbrk (0);
289 new_size = i - auxhdr.exec_dmem;
290
291 /* Copy the old file to the new, up to the data space */
292 lseek (old, 0, 0);
293 copy_file (old, new, auxhdr.exec_dfile);
294
295 /* Skip the old data segment and write a new one */
296 lseek (old, old_size, 1);
297 save_data_space (new, &hdr, &auxhdr, new_size);
298
299 /* Copy the rest of the file */
300 copy_rest (old, new);
301
302 /* Update file pointers since we probably changed size of data area */
303 update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
304
305 /* Save the modified header */
306 write_header (new, &hdr, &auxhdr);
307
308 /* Close the binary file */
309 close (old);
310 close (new);
311}