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