[TMP] enable load_prefer_newer
[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
d51c175a
PE
77/* Report a fatal error and exit. */
78static _Noreturn void
79unexec_error (char const *msg)
80{
81 perror (msg);
82 exit (1);
83}
84
85/* Do an lseek and check the result. */
86static void
87check_lseek (int fd, off_t offset, int whence)
88{
89 if (lseek (fd, offset, whence) < 0)
90 unexec_error ("Cannot lseek");
91}
92
89117df0
GM
93/* Save current data space in the file, update header. */
94
1dae0f0a
AS
95static void
96save_data_space (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
97 int size)
89117df0
GM
98{
99 /* Write the entire data space out to the file */
100 if (write (file, auxhdr->exec_dmem, size) != size)
d51c175a 101 unexec_error ("Can't save new data space");
89117df0
GM
102
103 /* Update the header to reflect the new data size */
104 auxhdr->exec_dsize = size;
105 auxhdr->exec_bsize = 0;
106}
107
108/* Update the values of file pointers when something is inserted. */
109
1dae0f0a
AS
110static void
111update_file_ptrs (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr,
112 unsigned int location, int offset)
89117df0
GM
113{
114 struct subspace_dictionary_record subspace;
115 int i;
116
117 /* Increase the overall size of the module */
118 hdr->som_length += offset;
119
120 /* Update the various file pointers in the header */
121#define update(ptr) if (ptr > location) ptr = ptr + offset
122 update (hdr->aux_header_location);
123 update (hdr->space_strings_location);
124 update (hdr->init_array_location);
125 update (hdr->compiler_location);
126 update (hdr->symbol_location);
127 update (hdr->fixup_request_location);
128 update (hdr->symbol_strings_location);
129 update (hdr->unloadable_sp_location);
130 update (auxhdr->exec_tfile);
131 update (auxhdr->exec_dfile);
132
133 /* Do for each subspace dictionary entry */
d51c175a 134 check_lseek (file, hdr->subspace_location, 0);
89117df0
GM
135 for (i = 0; i < hdr->subspace_total; i++)
136 {
d51c175a
PE
137 ptrdiff_t subspace_size = sizeof subspace;
138 if (read (file, &subspace, subspace_size) != subspace_size)
139 unexec_error ("Can't read subspace record");
89117df0
GM
140
141 /* If subspace has a file location, update it */
142 if (subspace.initialization_length > 0
143 && subspace.file_loc_init_value > location)
144 {
145 subspace.file_loc_init_value += offset;
d51c175a
PE
146 check_lseek (file, -subspace_size, 1);
147 if (write (file, &subspace, subspace_size) != subspace_size)
148 unexec_error ("Can't update subspace record");
89117df0
GM
149 }
150 }
151
152 /* Do for each initialization pointer record */
153 /* (I don't think it applies to executable files, only relocatables) */
154#undef update
155}
156
157/* Read in the header records from an a.out file. */
158
1dae0f0a
AS
159static void
160read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
161{
162
163 /* Read the header in */
d51c175a 164 check_lseek (file, 0, 0);
89117df0 165 if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
d51c175a 166 unexec_error ("Couldn't read header from a.out file");
89117df0
GM
167
168 if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC
169 && hdr->a_magic != DEMAND_MAGIC)
170 {
5af1f9fe 171 fprintf (stderr, "a.out file doesn't have valid magic number\n");
89117df0
GM
172 exit (1);
173 }
174
d51c175a 175 check_lseek (file, hdr->aux_header_location, 0);
89117df0 176 if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
d51c175a 177 unexec_error ("Couldn't read auxiliary header from a.out file");
89117df0
GM
178}
179
180/* Write out the header records into an a.out file. */
181
1dae0f0a
AS
182static void
183write_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
184{
185 /* Update the checksum */
186 hdr->checksum = calculate_checksum (hdr);
187
188 /* Write the header back into the a.out file */
d51c175a 189 check_lseek (file, 0, 0);
89117df0 190 if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr))
d51c175a
PE
191 unexec_error ("Couldn't write header to a.out file");
192 check_lseek (file, hdr->aux_header_location, 0);
89117df0 193 if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr))
d51c175a 194 unexec_error ("Couldn't write auxiliary header to a.out file");
89117df0
GM
195}
196
197/* Calculate the checksum of a SOM header record. */
198
1dae0f0a
AS
199static int
200calculate_checksum (struct header *hdr)
89117df0
GM
201{
202 int checksum, i, *ptr;
203
204 checksum = 0; ptr = (int *) hdr;
205
206 for (i = 0; i < sizeof (*hdr) / sizeof (int) - 1; i++)
207 checksum ^= ptr[i];
208
209 return (checksum);
210}
211
212/* Copy size bytes from the old file to the new one. */
213
1dae0f0a
AS
214static void
215copy_file (int old, int new, int size)
89117df0
GM
216{
217 int len;
218 int buffer[8192]; /* word aligned will be faster */
219
220 for (; size > 0; size -= len)
221 {
222 len = min (size, sizeof (buffer));
223 if (read (old, buffer, len) != len)
d51c175a 224 unexec_error ("Read failure on a.out file");
89117df0 225 if (write (new, buffer, len) != len)
d51c175a 226 unexec_error ("Write failure in a.out file");
89117df0
GM
227 }
228}
229
230/* Copy the rest of the file, up to EOF. */
231
1dae0f0a
AS
232static void
233copy_rest (int old, int new)
89117df0
GM
234{
235 int buffer[4096];
236 int len;
237
238 /* Copy bytes until end of file or error */
239 while ((len = read (old, buffer, sizeof (buffer))) > 0)
240 if (write (new, buffer, len) != len) break;
241
242 if (len != 0)
d51c175a 243 unexec_error ("Unable to copy the rest of the file");
89117df0
GM
244}
245
246#ifdef DEBUG
1dae0f0a
AS
247static void
248display_header (struct header *hdr, struct som_exec_auxhdr *auxhdr)
89117df0
GM
249{
250 /* Display the header information (debug) */
251 printf ("\n\nFILE HEADER\n");
252 printf ("magic number %d \n", hdr->a_magic);
253 printf ("text loc %.8x size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
254 printf ("data loc %.8x size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
255 printf ("entry %x \n", auxhdr->exec_entry);
256 printf ("Bss segment size %u\n", auxhdr->exec_bsize);
257 printf ("\n");
258 printf ("data file loc %d size %d\n",
259 auxhdr->exec_dfile, auxhdr->exec_dsize);
260 printf ("som_length %d\n", hdr->som_length);
261 printf ("unloadable sploc %d size %d\n",
262 hdr->unloadable_sp_location, hdr->unloadable_sp_size);
263}
264#endif /* DEBUG */
1dae0f0a
AS
265
266
267/* Create a new a.out file, same as old but with current data space */
268void
269unexec (const char *new_name, /* name of the new a.out file to be created */
270 const char *old_name) /* name of the old a.out file */
271{
272 int old, new;
273 int old_size, new_size;
274 struct header hdr;
275 struct som_exec_auxhdr auxhdr;
276 long i;
277
278 /* For the greatest flexibility, should create a temporary file in
279 the same directory as the new file. When everything is complete,
280 rename the temp file to the new name.
281 This way, a program could update its own a.out file even while
282 it is still executing. If problems occur, everything is still
283 intact. NOT implemented. */
284
d51c175a 285 /* Open the input and output a.out files. */
406af475 286 old = emacs_open (old_name, O_RDONLY, 0);
1dae0f0a 287 if (old < 0)
d51c175a 288 unexec_error (old_name);
406af475 289 new = emacs_open (new_name, O_CREAT | O_RDWR | O_TRUNC, 0777);
1dae0f0a 290 if (new < 0)
d51c175a 291 unexec_error (new_name);
1dae0f0a 292
d51c175a 293 /* Read the old headers. */
1dae0f0a
AS
294 read_header (old, &hdr, &auxhdr);
295
296 brk_on_dump = (long) sbrk (0);
297
d51c175a 298 /* Decide how large the new and old data areas are. */
1dae0f0a
AS
299 old_size = auxhdr.exec_dsize;
300 /* I suspect these two statements are separate
301 to avoid a compiler bug in hpux version 8. */
302 i = (long) sbrk (0);
303 new_size = i - auxhdr.exec_dmem;
304
d51c175a
PE
305 /* Copy the old file to the new, up to the data space. */
306 check_lseek (old, 0, 0);
1dae0f0a
AS
307 copy_file (old, new, auxhdr.exec_dfile);
308
d51c175a
PE
309 /* Skip the old data segment and write a new one. */
310 check_lseek (old, old_size, 1);
1dae0f0a
AS
311 save_data_space (new, &hdr, &auxhdr, new_size);
312
d51c175a 313 /* Copy the rest of the file. */
1dae0f0a
AS
314 copy_rest (old, new);
315
d51c175a 316 /* Update file pointers since we probably changed size of data area. */
1dae0f0a
AS
317 update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
318
d51c175a 319 /* Save the modified header. */
1dae0f0a
AS
320 write_header (new, &hdr, &auxhdr);
321
d51c175a 322 /* Close the binary file. */
bacba3c2
PE
323 emacs_close (old);
324 emacs_close (new);
1dae0f0a 325}