[comp.emacs] Emacs on an HP 9000 series 840

shaker@hpihoah.HP.COM (Chris Shaker) (06/10/88)

Gnu version 18.51 comes standard with the neccessary files to properly
compile and run on an HP9000 Series 800.

It came up just fine on the first try on our 850.  Just study INSTALL
before starting.

Chris Shaker

bd@hpsemc.HP.COM (bob desinger) (06/11/88)

Gnu Emacs 18.51 is alive and well on the 840 and all the other members
of the Series 800 family.  While I'm at it, here's another change to
make it run better on the s800 machines.

The unexec() for 18.51 in src/unexhp9ks800.c dropped a core after
about 15 minutes on my 2.0 system.  I replaced it with one written
by a colleague (after a little hacking), which has sustained heavy
abuse for about three weeks now.  The author gave me permission to
broadcast it, so here is the new unexhp9ks800.c.  (I'll send this to
FSF, too.)

This should work on older versions of Series 800 HP-UX, too.  This
version also allows you to use the symbolic debugger on emacs.  The
old one caused even venerable adb to dump core all over emacs's old
core file.  (Sort of making you lose the usefulness of the emacs core
file....) 

-- bd

#! /bin/sh
# This is a shell archive.  Remove anything before this line,
# then unwrap it by saving it in a file and typing "sh file".
#
# Wrapped by bd at hpsemc on Fri Jun 10 18:05:53 1988
# Contents:
#	unexhp9k800.c 	

PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH; export PATH
echo 'At the end, you should see the message "End of shell archive."'

echo Extracting unexhp9k800.c
cat >unexhp9k800.c <<'@//E*O*F unexhp9k800.c//'
/*********************************************************************
unexec saves the calling programs data area into an a.out file.

   It creates a copy of the old a.out file, and replaces the old data
   area with the current data area.  When the new file is executed, the
   process will see the same data structures and data values that the
   original process had when unexec was called.

   Unlike other versions of unexec, this one copies symbol table and
   debug information to the new a.out file.  Thus, the new a.out file
   may be debugged with symbolic debuggers.

   If you fix any bugs in this, I'd like to incorporate your fixes!
   Send them to uunet!hpda!hpsemc!jmorris or jmorris%hpsemc@hplabs.HP.COM.

CAVEATS:
   This routine saves the current value of all static and external
   variables.  This means that any data structure that needs to be
   initialized must be explicitly reset.  Variables will not have their
   expected default values.

   Unfortunately, the HP-UX signal handler has internal initialization
   flags which are not explicitly reset.  Thus, for signals to work in
   conjunction with this routine, the following code must executed when
   the new process starts up.

       void _sigreturn();
	  ...
       sigsetreturn(_sigreturn);


History
-------
880525 bob desinger Changed parameters to be compatible with Gnu Emacs,
                    added the new magic number for demand-loadable programs,
                    and (with John's guidance) fixed a dependency on
                    errno being zero before the routine is called.

880505 John Morris  Extensive rewriting to support source level debugging

880410 John Morris  Update the checksum in the a.out header.
                    Simplified the subroutine parameters.

880401 John Morris  Original version, like gnu-emacs version but keeps
                    the symbol table intact for adb type debugging.

***********************************************************************/
   

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

#include <a.out.h>

#define NBPG 2048
#define roundup(x,n) ( ( (x)+(n-1) ) & ~(n-1) )  /* n is power of 2 */
#define min(x,y)  ( ((x)<(y))?(x):(y) )



unexec(new_name, old_name, new_end_of_text, dummy1, dummy2)
/*************************************************************************
unexec creates a new a.out file, same as old, but with current data space
*************************************************************************/
    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 */
    {
    int old, new;
    int old_size, new_size;
    struct header hdr;
    struct som_exec_auxhdr auxhdr;

    /*********************************************************
    For the greatest flexibility, should create a temporary file
    in the same directory as the new file.  When everything
    is complete, then 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);

    /* decide how large the new and old data areas are */
    old_size = auxhdr.exec_dsize;
    new_size = sbrk(0) - 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);

    /* close the binary file */
    close(old);
    close(new);
    exit(0);
    }



save_data_space(file, hdr, auxhdr, size)
/**************************************************************
save_data_space saves the current data space in the file, updates header
************************************************************************/
    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);}
   
    /* update the header to reflect the new data size */
    auxhdr->exec_dsize = size;
    auxhdr->exec_bsize = 0;
    }


update_file_ptrs(file, hdr, auxhdr, location, offset)
/*******************************************************************
update_file_ptrs updates the values of file pointers when something is inserted
*************************************************************************/
    int file;
    struct header *hdr;
    struct som_exec_auxhdr *auxhdr;
    unsigned int location;
    int offset;
    {
    struct subspace_dictionary_record subspace;
    int i;

    /* increase the overall size of the module */
    hdr->som_length += offset;

    /* update the various file pointers in the header */
#define update(ptr) if (ptr > location) ptr = ptr + offset
    update(hdr->aux_header_location);
    update(hdr->space_strings_location);
    update(hdr->init_array_location);
    update(hdr->compiler_location);
    update(hdr->symbol_location);
    update(hdr->fixup_request_location);
    update(hdr->symbol_strings_location);
    update(hdr->unloadable_sp_location);
    update(auxhdr->exec_tfile);
    update(auxhdr->exec_dfile);

    /* do for each subspace dictionary entry */
    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);}

	/* 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);}
            }
        } 

    /* do for each initialization pointer record */
    /*  (Don't think it applies to executable files, only relocatibles) */

#undef update
    }

read_header(file, hdr, auxhdr)
/***************************************************************
read_header reads in the header records from an a.out file
***************************************************************/
    int file;
    struct header *hdr;
    struct som_exec_auxhdr *auxhdr;
    {

    /* read the header in */
    lseek(file, 0, 0);
    if (read(file, hdr, sizeof(*hdr)) != sizeof(*hdr))
        { perror("Couldn't read header from a.out file"); exit(1); }

    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"); 
        exit(1);  
        }

    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);
        }

    }


write_header(file, hdr, auxhdr)
/************************************************************************
write_header writes out the header records into an a.out file
************************************************************************/
    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);
    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);
    if (write(file, auxhdr, sizeof(*auxhdr)) != sizeof(*auxhdr))
        {perror("Couldn't write auxiliary header to a.out file");exit(1);}
    }


calculate_checksum(hdr)
/**********************************************************************
calculate_checksum calculates the checksum of a SOM header record
************************************************************************/
    struct header *hdr;
    {
    int checksum, i, *ptr;

    checksum = 0;  ptr = (int *) hdr;

    for (i=0; i<sizeof(*hdr)/sizeof(int)-1; i++)
	checksum ^= ptr[i];
    
    return(checksum);
    }




copy_file(old, new, size)
/******************************************************************
copy size bytes from the old file to the new one
********************************************************************/
    int new, old;
    int size;
    {
    int len;
    int buffer[8196];  /* word aligned will be faster */

    for (; size > 0; size -= len)
        {
	len = min(size, sizeof(buffer));
        if (read(old, buffer, len) != len)
	    { perror("Read failure on a.out file"); exit(1); }
        if (write(new, buffer, len) != len)
	    { perror("Write failure in a.out file"); exit(1); }
        }
    }


copy_rest(old, new)
/*********************************************************
copy_rest copies the rest of the file, up to EOF
***********************************************************/
    int new, old;
    {
    int buffer[4096];
    int len;

    /* copy bytes until end of file or error */
    while ( (len = read(old, buffer, sizeof(buffer))) > 0)
           if (write(new, buffer, len) != len) break;

    if (len != 0)
	{perror("Unable to copy the rest of the file"); exit(1);}

    }


display_header(hdr, auxhdr)
    struct header *hdr;
    struct som_exec_auxhdr *auxhdr;
    {

    /* display the header information (debug) */
    printf("\n\n  FILE HEADER\n");
    printf("magic number %d \n", hdr->a_magic); 
    printf("text loc %.8x   size %d \n", auxhdr->exec_tmem, auxhdr->exec_tsize);
    printf("data loc %.8x   size %d \n", auxhdr->exec_dmem, auxhdr->exec_dsize);
    printf("entry     %x \n",   auxhdr->exec_entry);
    printf("Bss  segment size %u\n", auxhdr->exec_bsize);
    printf("\n");
    printf("data file loc %d    size %d\n", 
	   auxhdr->exec_dfile, auxhdr->exec_dsize);
    printf("som_length %d\n", hdr->som_length);
    printf("unloadable sploc %d    size %d\n",
	   hdr->unloadable_sp_location, hdr->unloadable_sp_size);
    }
@//E*O*F unexhp9k800.c//

set `wc -lwc <unexhp9k800.c`
if test $1 -ne 329 -o $2 -ne 1224 -o $3 -ne 10924
then	echo ! unexhp9k800.c should have 329 lines, 1224 words, and 10924 characters
	echo ! but has $1 lines, $2 words, and $3 characters
fi
chmod 664 unexhp9k800.c

echo "End of shell archive."
exit 0