home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3637 < prev    next >
Encoding:
Internet Message Format  |  1991-07-17  |  6.4 KB

  1. From: news@and.cs.liv.ac.uk
  2. Newsgroups: alt.sources
  3. Subject: Undumper for HPUX 7.0
  4. Message-ID: <1991Jul16.101517.297@and.cs.liv.ac.uk>
  5. Date: 16 Jul 91 10:15:15 GMT
  6.  
  7. The below 'undump' program is for HPUX 7.xx Series 300 machines.
  8.  
  9. Undump enables you to create pre-loaded programs 
  10. e.g. a fast starting version of 'TeX' with pre-loaded 
  11. format file.
  12.  
  13. #---------------------------------- cut here ----------------------------------
  14. # This is a shell archive.  Remove anything before this line,
  15. # then unpack it by saving it in a file and typing "sh file".
  16. #
  17. # Wrapped by David Nixon <djn@di8> on Thu May 23 15:23:02 1991
  18. #
  19. # This archive contains:
  20. #    undump.c    
  21. #
  22. # Error checking via wc(1) will be performed.
  23.  
  24. LANG=""; export LANG
  25. PATH=/bin:/usr/bin:$PATH; export PATH
  26.  
  27. echo x - undump.c
  28. cat >undump.c <<'@EOF'
  29. /*  undump *  Ver. 1.2 : 1/11/90 *  Author: D.J.Nixon 
  30.  *
  31.  *  (C.) 1990 University of Liverpool
  32.  *
  33.  
  34. or HPUX on series 300 machines.  
  35.  * 
  36.  */
  37.  
  38. /*
  39.    Compilation: cc -O -DWOPR -o undump undump.c 
  40.  
  41.    Syntax: undump [b.out] [a.out] [core]
  42.  
  43.    Given an executable and its core image 'undump' will combine 
  44.    the executable's header and text with data from the core file
  45.    to build an "un-dumped" executable.
  46.  
  47.    Use to create "pre-loaded" programs such as 'TeX'.
  48.  
  49.    N.B. this version works with HPUX 7.0 shared, demand paged and non-shared executables.
  50. */
  51.  
  52. #ifndef WOPR
  53. #define WOPR
  54. #endif
  55. #include <stdio.h>  
  56. #include <fcntl.h>
  57. #include <signal.h>  
  58. #include <errno.h>
  59. #include <unistd.h>
  60. #include <sys/types.h>
  61. #include <sys/param.h>  
  62. #include <sys/user.h>
  63. #include <sys/sysmacros.h>
  64. #define USERPAGES (EXEC_PAGESIZE * UPAGES)    /* size of u-area in core file */
  65.  
  66.  
  67. extern int errno;    
  68. struct exec hdr;     
  69. struct user usr;    
  70. int ex,core,nex;   /* file descriptors */
  71. char *undumped;  
  72.  
  73. long lseek(), copyf();
  74. void fatal_err();  
  75. int handler();  
  76.  
  77. void main(argc, argv)
  78. _int argc;
  79. char *argv[4];
  80. {
  81. long offset,    
  82.      bytecount, 
  83.      orig_data_sz;
  84.   /* Open files and set signals. */
  85.   if (argc != 4) {
  86.     fprintf(stderr,"Usage: undump [b.out] [a.out] [core]\n"); exit(0);
  87.   }
  88.   if ((ex=open(argv[2], O_RDONLY)) == -1) {
  89.     fatal_err("Can't open a.out file"); 
  90.   }
  91.   if ((core=open(argv[3], O_RDONLY)) == -1) {
  92.     fatal_err("Can't open core file");
  93.   }
  94.   if ((nex=open(argv[1], O_WRONLY | O_CREAT | O_EXCL, 0700)) == -1) {
  95.     fatal_err("Can't create b.out file");
  96.   }  
  97.   signal(SIGQUIT, SIG_IGN);
  98.   signal(SIGINT, handler);
  99.   undumped=argv[1];
  100.   /* Read in a.out file header. */
  101.   if ((read(ex, &hdr, sizeof(struct exec))) < sizeof(struct exec)) 
  102.                            fatal_err("Could not read a.out file header");
  103.   /* Check a.out file is executable. */    
  104.   if (N_BADMAG(hdr)) fatal_err("Bad magic number");      
  105.   /* save this to get symtabs offset later */
  106.   orig_data_sz=hdr.a_data;
  107. udata,hdr.a_bss);
  108.   /* Read in user area to get size of b.out data segment */
  109.   if ((read(core, &usr, sizeof(struct user))) < sizeof(struct user))
  110.                           fatal_err("Could not read user area from core");
  111.   if (hdr.a_magic.file_type != EXEC_MAGIC)
  112.                           hdr.a_data=ctob(usr.u_dsize); /* exact */ 
  113.   /* Get size of core */
  114.   if ((offset=lseek(core, 0L, 2)) == -1) 
  115.                            fatal_err("Error seeking to end of core file");
  116.   printf("core : %u u_area : %u\n",offset,USERPAGES); 
  117.   if (hdr.a_magic.file_type == EXEC_MAGIC)
  118.                            hdr.a_data=(offset - (USERPAGES + hdr.a_text)); /* data plus shell env. */
  119.   hdr.a_bss=(0L);     /* zero length BSS segment */       
  120.   printf("b.out :- text %u data %u bss %u\n\n",hdr.a_text,hdr.a_data,hdr.a_bss);
  121.   /* Copy a.out header and text segment to b.out file. */
  122.   if ((write(nex, &hdr, sizeof(struct exec))) < sizeof(struct exec)) 
  123.  t"); 
  124.   lseek(ex, TEXT_OFFSET(hdr), 0); 
  125.   lseek(nex, TEXT_OFFSET(hdr), 0);     
  126.   printf("copying %u bytes of text\n",hdr.a_text);  
  127.   if ((bytecount=copyf(ex, nex, hdr.a_text)) < hdr.a_text ) {
  128.     if (bytecount < 0L) {
  129.       fatal_err("Error copying text segment to b.out");
  130.     } else {
  131.       fatal_err("At EOF in a.out text segment");
  132.     }  
  133.   }
  134.   /* Copy contiguous data/BSS segment from core file to b.out file data segment. */
  135.   if (hdr.a_magic.file_type ==  EXEC_MAGIC)    /* normal executable; text dumped */  
  136.     offset=(USERPAGES  + hdr.a_text); 
  137.   else
  138.     offset=(USERPAGES);                         /* no text dumped for shared or demand paged  */
  139.   lseek(core, offset, 0);   
  140.   lseek(nex, DATA_OFFSET(hdr), 0); 
  141.   printf("copying %u bytes of undumped data\n",hdr.a_data);  
  142.   if ((bytecount=copyf(core, nex, hdr.a_data)) < hdr.a_data) {
  143.     if (bytecount < 0L) {
  144.       fatal_err("Error copying data segment to b.out");
  145.     } else { 
  146.       fatal_err("At EOF in core");
  147.     }
  148.   } 
  149.   /* Copy symbol table and debugging info. onto end of b.out file */
  150.   {
  151.   long symtabs_sz=(hdr.a_trsize + hdr.a_drsize + hdr.a_pasint + hdr.a_lesyms + hdr.a_dnttsize + hdr.a_sltsize + hdr.a_vtsize);
  152.     hdr.a_data=orig_data_sz;
  153.     lseek(ex, MODCAL_OFFSET(hdr), 0); 
  154.     printf("copying %u bytes of symbol data\n",symtabs_sz);  
  155.     if ((bytecount=copyf(ex, nex, symtabs_sz)) < symtabs_sz) {
  156.       if (bytecount < 0L) {
  157.         fatal_err("Error copying symbol tables to b.out");
  158.       } else { 
  159.         fatal_err("At EOF in a.out");
  160.       }
  161.     } 
  162.   }
  163.   puts("\nundump done\n");
  164.   exit(0);
  165. } /* main */
  166.  
  167. /* 
  168.  * copyf
  169.  *
  170.  * Copies specified number of bytes from file f1 to file f2.
  171.  *
  172.  */
  173. long copyf(f1,f2,bytes)
  174. int f1,f2;
  175. long bytes;
  176. {
  177. char buf[BUFSIZ]; 
  178. register long count=0L;
  179. register int n,diff;
  180.   while((n=read(f1, buf, BUFSIZ)) > 0) {
  181.     count+=n;
  182.     if (count > bytes) {
  183.       diff=(count - bytes);
  184.       if (lseek(f1, -(diff), 1) == -1)  {
  185.         return(-3L);
  186.       }
  187.       n-=(diff);
  188.       count-=(diff);
  189.     }
  190.     if (write(f2, buf, n) == -1) {
  191.       return(-2L);                          
  192.     }
  193.     if (count >= bytes) {
  194.       break;
  195.     }
  196.   } 
  197.   if (n == -1) {
  198.     return(-1L);
  199.   } else {
  200.     return(count);
  201.   }
  202. }
  203.  
  204. /* 
  205.  * fatal_err 
  206.  *
  207.  */
  208. void fatal_err(message)
  209. char *message;
  210. {
  211.   if (errno) perror(message);
  212.   else
  213.              fprintf(stderr,"%s\n",message);
  214.   close(nex);
  215.   unlink(undumped);   
  216.   exit(1);
  217. }
  218.  
  219. /*
  220.  * handler 
  221.  *
  222.  * Removes b.out file on interrupt.
  223.  *
  224.  */
  225. handler(sig)
  226. int sig;
  227. {
  228.   puts("Interrupt");
  229.   close(nex);
  230.   unlink(undumped);
  231.   exit(1);   
  232. }
  233. @EOF
  234. set `wc -lwc <undump.c`
  235. if test $1$2$3 != 2037115689
  236. then
  237.     echo ERROR: wc results of undump.c are $* should be 203 711 5689
  238. fi
  239.  
  240. chmod 644 undump.c
  241.  
  242. exit 0
  243.