* admin/admin.el (make-manuals): Avoid hard-coding list of misc manuals.
[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
PE
53#include "unexec.h"
54
89117df0
GM
55#include <stdio.h>
56#include <fcntl.h>
57#include <errno.h>
89117df0 58#include <a.out.h>
89117df0 59#include <dl.h>
89117df0
GM
60
61/* brk value to restore, stored as a global.
62 This is really used only if we used shared libraries. */
63static long brk_on_dump = 0;
64
65/* Called from main, if we use shared libraries. */
66int
1dae0f0a 67run_time_remap (char *ignored)
89117df0
GM
68{
69 brk ((char *) brk_on_dump);
70}
71
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))
75
89117df0
GM
76/* Save current data space in the file, update header. */
77
1dae0f0a
AS
78static void
79save_data_space (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
80 int size)
89117df0
GM
81{
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); }
85
86 /* Update the header to reflect the new data size */
87 auxhdr->exec_dsize = size;
88 auxhdr->exec_bsize = 0;
89}
90
91/* Update the values of file pointers when something is inserted. */
92
1dae0f0a
AS
93static void
94update_file_ptrs (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
95 unsigned int location, int offset)
89117df0
GM
96{
97 struct subspace_dictionary_record subspace;
98 int i;
99
100 /* Increase the overall size of the module */
101 hdr->som_length += offset;
102
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);
115
116 /* Do for each subspace dictionary entry */
117 lseek (file, hdr->subspace_location, 0);
118 for (i = 0; i < hdr->subspace_total; i++)
119 {
120 if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace))
121 { perror ("Can't read subspace record"); exit (1); }
122
123 /* If subspace has a file location, update it */
124 if (subspace.initialization_length > 0
125 && subspace.file_loc_init_value > location)
126 {
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); }
131 }
132 }
133
134 /* Do for each initialization pointer record */
135 /* (I don't think it applies to executable files, only relocatables) */
136#undef update
137}
138
139/* Read in the header records from an a.out file. */
140
1dae0f0a
AS
141static void
142read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
143{
144
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); }
149
150 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
151 && hdr->a_magic != DEMAND_MAGIC)
152 {
5af1f9fe 153 fprintf (stderr, "a.out file doesn't have valid magic number\n");
89117df0
GM
154 exit (1);
155 }
156
157 lseek (file, hdr->aux_header_location, 0);
158 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
159 {
160 perror ("Couldn't read auxiliary header from a.out file");
161 exit (1);
162 }
163}
164
165/* Write out the header records into an a.out file. */
166
1dae0f0a
AS
167static void
168write_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
169{
170 /* Update the checksum */
171 hdr->checksum = calculate_checksum (hdr);
172
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); }
180}
181
182/* Calculate the checksum of a SOM header record. */
183
1dae0f0a
AS
184static int
185calculate_checksum (struct header *hdr)
89117df0
GM
186{
187 int checksum, i, *ptr;
188
189 checksum = 0; ptr = (int *) hdr;
190
191 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
192 checksum ^= ptr[i];
193
194 return (checksum);
195}
196
197/* Copy size bytes from the old file to the new one. */
198
1dae0f0a
AS
199static void
200copy_file (int old, int new, int size)
89117df0
GM
201{
202 int len;
203 int buffer[8192]; /* word aligned will be faster */
204
205 for (; size > 0; size -= len)
206 {
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); }
212 }
213}
214
215/* Copy the rest of the file, up to EOF. */
216
1dae0f0a
AS
217static void
218copy_rest (int old, int new)
89117df0
GM
219{
220 int buffer[4096];
221 int len;
222
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;
226
227 if (len != 0)
228 { perror ("Unable to copy the rest of the file"); exit (1); }
229}
230
231#ifdef DEBUG
1dae0f0a
AS
232static void
233display_header (struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
234{
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);
248}
249#endif /* DEBUG */
1dae0f0a
AS
250
251
252/* Create a new a.out file, same as old but with current data space */
253void
254unexec (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 */
256{
257 int old, new;
258 int old_size, new_size;
259 struct header hdr;
260 struct som_exec_auxhdr auxhdr;
261 long i;
262
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. */
269
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); }
277
278 /* Read the old headers */
279 read_header (old, &hdr, &auxhdr);
280
281 brk_on_dump = (long) sbrk (0);
282
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;
289
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);
293
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);
297
298 /* Copy the rest of the file */
299 copy_rest (old, new);
300
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);
303
304 /* Save the modified header */
305 write_header (new, &hdr, &auxhdr);
306
307 /* Close the binary file */
308 close (old);
309 close (new);
310}