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