1 /* Unexec for HP 9000 Series 800 machines.
2 Bob Desinger <hpsemc!bd@hplabs.hp.com>
4 Note that the GNU project considers support for HP operation a
5 peripheral activity which should not be allowed to divert effort
6 from development of the GNU system. Changes in this code will be
7 installed when users send them in, but aside from that we don't
8 plan to think about it, or about whether other Emacs maintenance
12 Unexec creates a copy of the old a.out file, and replaces the old data
13 area with the current data area. When the new file is executed, the
14 process will see the same data structures and data values that the
15 original process had when unexec was called.
17 Unlike other versions of unexec, this one copies symbol table and
18 debug information to the new a.out file. Thus, the new a.out file
19 may be debugged with symbolic debuggers.
21 If you fix any bugs in this, I'd like to incorporate your fixes.
22 Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.
25 This routine saves the current value of all static and external
26 variables. This means that any data structure that needs to be
27 initialized must be explicitly reset. Variables will not have their
28 expected default values.
30 Unfortunately, the HP-UX signal handler has internal initialization
31 flags which are not explicitly reset. Thus, for signals to work in
32 conjunction with this routine, the following code must executed when
33 the new process starts up.
37 sigsetreturn(_sigreturn);
47 #define roundup(x,n) ( ( (x)+(n-1) ) & ~(n-1) ) /* n is power of 2 */
48 #define min(x,y) ( ((x)<(y))?(x):(y) )
51 /* Create a new a.out file, same as old but with current data space */
53 unexec(new_name
, old_name
, new_end_of_text
, dummy1
, dummy2
)
54 char new_name
[]; /* name of the new a.out file to be created */
55 char old_name
[]; /* name of the old a.out file */
56 char *new_end_of_text
; /* ptr to new edata/etext; NOT USED YET */
57 int dummy1
, dummy2
; /* not used by emacs */
60 int old_size
, new_size
;
62 struct som_exec_auxhdr auxhdr
;
64 /* For the greatest flexibility, should create a temporary file in
65 the same directory as the new file. When everything is complete,
66 rename the temp file to the new name.
67 This way, a program could update its own a.out file even while
68 it is still executing. If problems occur, everything is still
69 intact. NOT implemented. */
71 /* Open the input and output a.out files */
72 old
= open(old_name
, O_RDONLY
);
74 { perror(old_name
); exit(1); }
75 new = open(new_name
, O_CREAT
|O_RDWR
|O_TRUNC
, 0777);
77 { perror(new_name
); exit(1); }
79 /* Read the old headers */
80 read_header(old
, &hdr
, &auxhdr
);
82 /* Decide how large the new and old data areas are */
83 old_size
= auxhdr
.exec_dsize
;
84 new_size
= sbrk(0) - auxhdr
.exec_dmem
;
86 /* Copy the old file to the new, up to the data space */
88 copy_file(old
, new, auxhdr
.exec_dfile
);
90 /* Skip the old data segment and write a new one */
91 lseek(old
, old_size
, 1);
92 save_data_space(new, &hdr
, &auxhdr
, new_size
);
94 /* Copy the rest of the file */
97 /* Update file pointers since we probably changed size of data area */
98 update_file_ptrs(new, &hdr
, &auxhdr
, auxhdr
.exec_dfile
, new_size
-old_size
);
100 /* Save the modified header */
101 write_header(new, &hdr
, &auxhdr
);
103 /* Close the binary file */
109 /* Save current data space in the file, update header. */
111 save_data_space(file
, hdr
, auxhdr
, size
)
114 struct som_exec_auxhdr
*auxhdr
;
117 /* Write the entire data space out to the file */
118 if (write(file
, auxhdr
->exec_dmem
, size
) != size
)
119 { perror("Can't save new data space"); exit(1); }
121 /* Update the header to reflect the new data size */
122 auxhdr
->exec_dsize
= size
;
123 auxhdr
->exec_bsize
= 0;
126 /* Update the values of file pointers when something is inserted. */
128 update_file_ptrs(file
, hdr
, auxhdr
, location
, offset
)
131 struct som_exec_auxhdr
*auxhdr
;
132 unsigned int location
;
135 struct subspace_dictionary_record subspace
;
138 /* Increase the overall size of the module */
139 hdr
->som_length
+= offset
;
141 /* Update the various file pointers in the header */
142 #define update(ptr) if (ptr > location) ptr = ptr + offset
143 update(hdr
->aux_header_location
);
144 update(hdr
->space_strings_location
);
145 update(hdr
->init_array_location
);
146 update(hdr
->compiler_location
);
147 update(hdr
->symbol_location
);
148 update(hdr
->fixup_request_location
);
149 update(hdr
->symbol_strings_location
);
150 update(hdr
->unloadable_sp_location
);
151 update(auxhdr
->exec_tfile
);
152 update(auxhdr
->exec_dfile
);
154 /* Do for each subspace dictionary entry */
155 lseek(file
, hdr
->subspace_location
, 0);
156 for (i
= 0; i
< hdr
->subspace_total
; i
++)
158 if (read(file
, &subspace
, sizeof(subspace
)) != sizeof(subspace
))
159 { perror("Can't read subspace record"); exit(1); }
161 /* If subspace has a file location, update it */
162 if (subspace
.initialization_length
> 0
163 && subspace
.file_loc_init_value
> location
)
165 subspace
.file_loc_init_value
+= offset
;
166 lseek(file
, -sizeof(subspace
), 1);
167 if (write(file
, &subspace
, sizeof(subspace
)) != sizeof(subspace
))
168 { perror("Can't update subspace record"); exit(1); }
172 /* Do for each initialization pointer record */
173 /* (I don't think it applies to executable files, only relocatables) */
177 /* Read in the header records from an a.out file. */
179 read_header(file
, hdr
, auxhdr
)
182 struct som_exec_auxhdr
*auxhdr
;
185 /* Read the header in */
187 if (read(file
, hdr
, sizeof(*hdr
)) != sizeof(*hdr
))
188 { perror("Couldn't read header from a.out file"); exit(1); }
190 if (hdr
->a_magic
!= EXEC_MAGIC
&& hdr
->a_magic
!= SHARE_MAGIC
191 && hdr
->a_magic
!= DEMAND_MAGIC
)
193 fprintf(stderr
, "a.out file doesn't have legal magic number\n");
197 lseek(file
, hdr
->aux_header_location
, 0);
198 if (read(file
, auxhdr
, sizeof(*auxhdr
)) != sizeof(*auxhdr
))
200 perror("Couldn't read auxiliary header from a.out file");
205 /* Write out the header records into an a.out file. */
207 write_header(file
, hdr
, auxhdr
)
210 struct som_exec_auxhdr
*auxhdr
;
212 /* Update the checksum */
213 hdr
->checksum
= calculate_checksum(hdr
);
215 /* Write the header back into the a.out file */
217 if (write(file
, hdr
, sizeof(*hdr
)) != sizeof(*hdr
))
218 { perror("Couldn't write header to a.out file"); exit(1); }
219 lseek(file
, hdr
->aux_header_location
, 0);
220 if (write(file
, auxhdr
, sizeof(*auxhdr
)) != sizeof(*auxhdr
))
221 { perror("Couldn't write auxiliary header to a.out file"); exit(1); }
224 /* Calculate the checksum of a SOM header record. */
226 calculate_checksum(hdr
)
229 int checksum
, i
, *ptr
;
231 checksum
= 0; ptr
= (int *) hdr
;
233 for (i
=0; i
<sizeof(*hdr
)/sizeof(int)-1; i
++)
239 /* Copy size bytes from the old file to the new one. */
241 copy_file(old
, new, size
)
246 int buffer
[8196]; /* word aligned will be faster */
248 for (; size
> 0; size
-= len
)
250 len
= min(size
, sizeof(buffer
));
251 if (read(old
, buffer
, len
) != len
)
252 { perror("Read failure on a.out file"); exit(1); }
253 if (write(new, buffer
, len
) != len
)
254 { perror("Write failure in a.out file"); exit(1); }
258 /* Copy the rest of the file, up to EOF. */
266 /* Copy bytes until end of file or error */
267 while ( (len
= read(old
, buffer
, sizeof(buffer
))) > 0)
268 if (write(new, buffer
, len
) != len
) break;
271 { perror("Unable to copy the rest of the file"); exit(1); }
275 display_header(hdr
, auxhdr
)
277 struct som_exec_auxhdr
*auxhdr
;
279 /* Display the header information (debug) */
280 printf("\n\nFILE HEADER\n");
281 printf("magic number %d \n", hdr
->a_magic
);
282 printf("text loc %.8x size %d \n", auxhdr
->exec_tmem
, auxhdr
->exec_tsize
);
283 printf("data loc %.8x size %d \n", auxhdr
->exec_dmem
, auxhdr
->exec_dsize
);
284 printf("entry %x \n", auxhdr
->exec_entry
);
285 printf("Bss segment size %u\n", auxhdr
->exec_bsize
);
287 printf("data file loc %d size %d\n",
288 auxhdr
->exec_dfile
, auxhdr
->exec_dsize
);
289 printf("som_length %d\n", hdr
->som_length
);
290 printf("unloadable sploc %d size %d\n",
291 hdr
->unloadable_sp_location
, hdr
->unloadable_sp_size
);