X-Git-Url: https://git.hcoop.net/bpt/emacs.git/blobdiff_plain/89117df0bb1c0803d1091873a330780ce8941543..0b5616fc74fe74464fa8873468e1e63924db6daa:/src/unexhp9k800.c diff --git a/src/unexhp9k800.c b/src/unexhp9k800.c index 495094029d..55db8071b7 100644 --- a/src/unexhp9k800.c +++ b/src/unexhp9k800.c @@ -49,19 +49,15 @@ sigsetreturn (_sigreturn); */ -#ifdef emacs #include -#endif +#include "unexec.h" +#include "lisp.h" #include #include #include - #include - -#ifdef HPUX_USE_SHLIBS #include -#endif /* brk value to restore, stored as a global. This is really used only if we used shared libraries. */ @@ -69,8 +65,7 @@ static long brk_on_dump = 0; /* Called from main, if we use shared libraries. */ int -run_time_remap (ignored) - char *ignored; +run_time_remap (char *ignored) { brk ((char *) brk_on_dump); } @@ -79,82 +74,31 @@ run_time_remap (ignored) #define roundup(x,n) (((x) + ((n) - 1)) & ~((n) - 1)) /* n is power of 2 */ #define min(x,y) (((x) < (y)) ? (x) : (y)) - -/* Create a new a.out file, same as old but with current data space */ - -unexec (new_name, old_name, new_end_of_text, dummy1, dummy2) - char new_name[]; /* name of the new a.out file to be created */ - char old_name[]; /* name of the old a.out file */ - char *new_end_of_text; /* ptr to new edata/etext; NOT USED YET */ - int dummy1, dummy2; /* not used by emacs */ +/* Report a fatal error and exit. */ +static _Noreturn void +unexec_error (char const *msg) { - int old, new; - int old_size, new_size; - struct header hdr; - struct som_exec_auxhdr auxhdr; - long i; - - /* For the greatest flexibility, should create a temporary file in - the same directory as the new file. When everything is complete, - rename the temp file to the new name. - This way, a program could update its own a.out file even while - it is still executing. If problems occur, everything is still - intact. NOT implemented. */ - - /* Open the input and output a.out files */ - old = open (old_name, O_RDONLY); - if (old < 0) - { perror (old_name); exit (1); } - new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777); - if (new < 0) - { perror (new_name); exit (1); } - - /* Read the old headers */ - read_header (old, &hdr, &auxhdr); - - brk_on_dump = (long) sbrk (0); - - /* Decide how large the new and old data areas are */ - old_size = auxhdr.exec_dsize; - /* I suspect these two statements are separate - to avoid a compiler bug in hpux version 8. */ - i = (long) sbrk (0); - new_size = i - auxhdr.exec_dmem; - - /* Copy the old file to the new, up to the data space */ - lseek (old, 0, 0); - copy_file (old, new, auxhdr.exec_dfile); - - /* Skip the old data segment and write a new one */ - lseek (old, old_size, 1); - save_data_space (new, &hdr, &auxhdr, new_size); - - /* Copy the rest of the file */ - copy_rest (old, new); - - /* Update file pointers since we probably changed size of data area */ - update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size); - - /* Save the modified header */ - write_header (new, &hdr, &auxhdr); + perror (msg); + exit (1); +} - /* Close the binary file */ - close (old); - close (new); - return 0; +/* Do an lseek and check the result. */ +static void +check_lseek (int fd, off_t offset, int whence) +{ + if (lseek (fd, offset, whence) < 0) + unexec_error ("Cannot lseek"); } /* Save current data space in the file, update header. */ -save_data_space (file, hdr, auxhdr, size) - int file; - struct header *hdr; - struct som_exec_auxhdr *auxhdr; - int size; +static void +save_data_space (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr, + int size) { /* Write the entire data space out to the file */ if (write (file, auxhdr->exec_dmem, size) != size) - { perror ("Can't save new data space"); exit (1); } + unexec_error ("Can't save new data space"); /* Update the header to reflect the new data size */ auxhdr->exec_dsize = size; @@ -163,12 +107,9 @@ save_data_space (file, hdr, auxhdr, size) /* Update the values of file pointers when something is inserted. */ -update_file_ptrs (file, hdr, auxhdr, location, offset) - int file; - struct header *hdr; - struct som_exec_auxhdr *auxhdr; - unsigned int location; - int offset; +static void +update_file_ptrs (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr, + unsigned int location, int offset) { struct subspace_dictionary_record subspace; int i; @@ -190,20 +131,21 @@ update_file_ptrs (file, hdr, auxhdr, location, offset) update (auxhdr->exec_dfile); /* Do for each subspace dictionary entry */ - lseek (file, hdr->subspace_location, 0); + check_lseek (file, hdr->subspace_location, 0); for (i = 0; i < hdr->subspace_total; i++) { - if (read (file, &subspace, sizeof (subspace)) != sizeof (subspace)) - { perror ("Can't read subspace record"); exit (1); } + ptrdiff_t subspace_size = sizeof subspace; + if (read (file, &subspace, subspace_size) != subspace_size) + unexec_error ("Can't read subspace record"); /* If subspace has a file location, update it */ if (subspace.initialization_length > 0 && subspace.file_loc_init_value > location) { subspace.file_loc_init_value += offset; - lseek (file, -sizeof (subspace), 1); - if (write (file, &subspace, sizeof (subspace)) != sizeof (subspace)) - { perror ("Can't update subspace record"); exit (1); } + check_lseek (file, -subspace_size, 1); + if (write (file, &subspace, subspace_size) != subspace_size) + unexec_error ("Can't update subspace record"); } } @@ -214,55 +156,48 @@ update_file_ptrs (file, hdr, auxhdr, location, offset) /* Read in the header records from an a.out file. */ -read_header (file, hdr, auxhdr) - int file; - struct header *hdr; - struct som_exec_auxhdr *auxhdr; +static void +read_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr) { /* Read the header in */ - lseek (file, 0, 0); + check_lseek (file, 0, 0); if (read (file, hdr, sizeof (*hdr)) != sizeof (*hdr)) - { perror ("Couldn't read header from a.out file"); exit (1); } + unexec_error ("Couldn't read header from a.out file"); if (hdr->a_magic != EXEC_MAGIC && hdr->a_magic != SHARE_MAGIC && hdr->a_magic != DEMAND_MAGIC) { - fprintf (stderr, "a.out file doesn't have legal magic number\n"); + fprintf (stderr, "a.out file doesn't have valid magic number\n"); exit (1); } - lseek (file, hdr->aux_header_location, 0); + check_lseek (file, hdr->aux_header_location, 0); if (read (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr)) - { - perror ("Couldn't read auxiliary header from a.out file"); - exit (1); - } + unexec_error ("Couldn't read auxiliary header from a.out file"); } /* Write out the header records into an a.out file. */ -write_header (file, hdr, auxhdr) - int file; - struct header *hdr; - struct som_exec_auxhdr *auxhdr; +static void +write_header (int file, struct header *hdr, struct som_exec_auxhdr *auxhdr) { /* Update the checksum */ hdr->checksum = calculate_checksum (hdr); /* Write the header back into the a.out file */ - lseek (file, 0, 0); + check_lseek (file, 0, 0); if (write (file, hdr, sizeof (*hdr)) != sizeof (*hdr)) - { perror ("Couldn't write header to a.out file"); exit (1); } - lseek (file, hdr->aux_header_location, 0); + unexec_error ("Couldn't write header to a.out file"); + check_lseek (file, hdr->aux_header_location, 0); if (write (file, auxhdr, sizeof (*auxhdr)) != sizeof (*auxhdr)) - { perror ("Couldn't write auxiliary header to a.out file"); exit (1); } + unexec_error ("Couldn't write auxiliary header to a.out file"); } /* Calculate the checksum of a SOM header record. */ -calculate_checksum (hdr) - struct header *hdr; +static int +calculate_checksum (struct header *hdr) { int checksum, i, *ptr; @@ -276,9 +211,8 @@ calculate_checksum (hdr) /* Copy size bytes from the old file to the new one. */ -copy_file (old, new, size) - int new, old; - int size; +static void +copy_file (int old, int new, int size) { int len; int buffer[8192]; /* word aligned will be faster */ @@ -287,16 +221,16 @@ copy_file (old, new, size) { len = min (size, sizeof (buffer)); if (read (old, buffer, len) != len) - { perror ("Read failure on a.out file"); exit (1); } + unexec_error ("Read failure on a.out file"); if (write (new, buffer, len) != len) - { perror ("Write failure in a.out file"); exit (1); } + unexec_error ("Write failure in a.out file"); } } /* Copy the rest of the file, up to EOF. */ -copy_rest (old, new) - int new, old; +static void +copy_rest (int old, int new) { int buffer[4096]; int len; @@ -306,13 +240,12 @@ copy_rest (old, new) if (write (new, buffer, len) != len) break; if (len != 0) - { perror ("Unable to copy the rest of the file"); exit (1); } + unexec_error ("Unable to copy the rest of the file"); } #ifdef DEBUG -display_header (hdr, auxhdr) - struct header *hdr; - struct som_exec_auxhdr *auxhdr; +static void +display_header (struct header *hdr, struct som_exec_auxhdr *auxhdr) { /* Display the header information (debug) */ printf ("\n\nFILE HEADER\n"); @@ -330,5 +263,63 @@ display_header (hdr, auxhdr) } #endif /* DEBUG */ -/* arch-tag: d55a09ac-9427-4ec4-8496-cb9d7710774f - (do not change this comment) */ + +/* Create a new a.out file, same as old but with current data space */ +void +unexec (const char *new_name, /* name of the new a.out file to be created */ + const char *old_name) /* name of the old a.out file */ +{ + int old, new; + int old_size, new_size; + struct header hdr; + struct som_exec_auxhdr auxhdr; + long i; + + /* For the greatest flexibility, should create a temporary file in + the same directory as the new file. When everything is complete, + rename the temp file to the new name. + This way, a program could update its own a.out file even while + it is still executing. If problems occur, everything is still + intact. NOT implemented. */ + + /* Open the input and output a.out files. */ + old = emacs_open (old_name, O_RDONLY, 0); + if (old < 0) + unexec_error (old_name); + new = emacs_open (new_name, O_CREAT | O_RDWR | O_TRUNC, 0777); + if (new < 0) + unexec_error (new_name); + + /* Read the old headers. */ + read_header (old, &hdr, &auxhdr); + + brk_on_dump = (long) sbrk (0); + + /* Decide how large the new and old data areas are. */ + old_size = auxhdr.exec_dsize; + /* I suspect these two statements are separate + to avoid a compiler bug in hpux version 8. */ + i = (long) sbrk (0); + new_size = i - auxhdr.exec_dmem; + + /* Copy the old file to the new, up to the data space. */ + check_lseek (old, 0, 0); + copy_file (old, new, auxhdr.exec_dfile); + + /* Skip the old data segment and write a new one. */ + check_lseek (old, old_size, 1); + save_data_space (new, &hdr, &auxhdr, new_size); + + /* Copy the rest of the file. */ + copy_rest (old, new); + + /* Update file pointers since we probably changed size of data area. */ + update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size); + + /* Save the modified header. */ + write_header (new, &hdr, &auxhdr); + + /* Close the binary file. */ + emacs_close (old); + emacs_close (new); +}