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