home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / undump-386i < prev    next >
Encoding:
Text File  |  1989-05-08  |  6.9 KB  |  316 lines

  1. Path: uunet!lll-winken!ames!oliveb!apple!rutgers!aramis.rutgers.edu!mcgrew
  2. From: mcgrew@aramis.rutgers.edu (Charles Mcgrew)
  3. Newsgroups: comp.sources.sun
  4. Subject: v01i001:  undump for 386i's
  5. Message-ID: <May.5.13.26.09.1989.12142@aramis.rutgers.edu>
  6. Date: 5 May 89 17:26:10 GMT
  7. Organization: Rutgers Univ., New Brunswick, N.J.
  8. Lines: 305
  9. Approved: mcgrew@aramis.rutgers.edu
  10.  
  11. Submitted-by: mcvax!hslrswi!mwm@uunet.uu.net (Mike McGann)
  12. Posting-number: Volume 1, Issue 1
  13. Archive-name: undump-386i
  14.  
  15. # remove everything above the line that says '/bin/sh' and feed 
  16. # it to sh.
  17.  
  18. #!/bin/sh
  19. #
  20. #
  21. #   This is for a 386i, which unfortunately we don't have at Rutgers.
  22. # I cannot therefore vouch for its working-ness.
  23. #
  24. #Charles
  25. #
  26. # This is a shell archive, meaning:
  27. # 1. Remove everything above the #! /bin/sh line.
  28. # 2. Save the resulting text in a file.
  29. # 3. Execute the file with /bin/sh (not csh) to create:
  30. #    undump.SYS_V.c
  31. # This archive created: Fri Jan 13 16:26:50 1989
  32. export PATH; PATH=/bin:/usr/bin:$PATH
  33. if test -f 'undump.SYS_V.c'
  34. then
  35.     echo shar: "will not over-write existing file 'undump.SYS_V.c'"
  36. else
  37. cat << \SHAR_EOF > 'undump.SYS_V.c'
  38. /*
  39.  * undump - resurrect a core file into a running program.
  40.  *
  41.  *    for UNIX System V on a 3Bx
  42.  *    that uses the Common Object File Format
  43.  *
  44.  * Author:
  45.  *    Lou Salkind
  46.  *    New York University
  47.  *    Tue Mar  3 13:18:25 EST 1987
  48.  *
  49.  * Adapted from:
  50.  *     Spencer Thomas's undump and the file unexec.c in GNU emacs
  51.  */
  52.  
  53. #include <sys/param.h>
  54. #include <sys/types.h>
  55. #ifndef i386
  56. #include <sys/psw.h>
  57. #include <sys/pcb.h>
  58. #include <sys/fs/s5dir.h>
  59. #endif
  60. #ifdef i386
  61. #include <sys/core.h>
  62. #endif
  63. #include <sys/signal.h>
  64. #include <sys/user.h>
  65.  
  66. #include <stdio.h>
  67. #include <sys/stat.h>
  68.  
  69. #include <aouthdr.h>
  70. #include <filehdr.h>
  71. #include <scnhdr.h>
  72. #include <syms.h>
  73.  
  74. #define    PAGE_SIZE    NBPC
  75.  
  76. struct filehdr fh;
  77. AOUTHDR aout;
  78. struct scnhdr tsc;
  79. struct scnhdr dsc;
  80. struct scnhdr bsc;
  81.  
  82. long bias;
  83. long lnnoptr;
  84. long text_scnptr;
  85. long data_scnptr;
  86. long symlocptr;
  87.  
  88. main(argc, argv)
  89.     char **argv;
  90. {
  91.     FILE *afp, *cfp, *nfp;
  92.     struct user u;
  93. #ifdef i386
  94.     struct core c;
  95. #endif
  96.     long off;
  97.     long size;
  98.     struct scnhdr sc;
  99.     int i, n;
  100.     char *a_out_name = "a.out";
  101.     char *core_name = "core";
  102.     char *new_name;
  103.  
  104.     if (argc < 2 || argc > 4) {
  105.         fprintf(stderr, "usage: %s new [a.out [core]]\n", argv[0]);
  106.         exit(1);
  107.     }
  108.     new_name = argv[1];
  109.     if (argc > 2)
  110.         a_out_name = argv[2];
  111.     if (argc > 3)
  112.         core_name = argv[3];
  113.     afp = fopen(a_out_name, "r");
  114.     if (afp == 0)
  115.         Perror(a_out_name);
  116.     cfp = fopen(core_name, "r");
  117.     if (cfp == 0)
  118.         Perror(core_name);
  119.     nfp = fopen(new_name, "w");
  120.     if (nfp == 0)
  121.         Perror(new_name);
  122.  
  123.     if (fread(&fh, sizeof fh, 1, afp) != 1)
  124.         Perror("fh read");
  125.     if (fread(&aout, sizeof aout, 1, afp) != 1)
  126.         Perror("aout read");
  127.  
  128.     for (i = 0; i < fh.f_nscns; i++) {
  129.         if (fread(&sc, sizeof(sc), 1, afp) != 1)
  130.             Perror("read");
  131.         if (strcmp(sc.s_name, ".text") == 0) {
  132.             tsc = sc;
  133.         } else if (strcmp(sc.s_name, ".data") == 0) {
  134.             dsc = sc;
  135.         } else if (strcmp(sc.s_name, ".bss") == 0) {
  136.             bsc = sc;
  137.         }
  138.     }
  139.  
  140. #ifndef i386
  141.     if (fread(&u, sizeof u, 1, cfp) != 1)
  142.         Perror("core read");
  143.     if (u.u_exdata.ux_tsize != aout.tsize ||
  144.         u.u_exdata.ux_dsize != aout.dsize ||
  145.         u.u_exdata.ux_bsize != aout.bsize) {
  146.         fprintf("mismatch between %s and %s sizes\n", a_out_name, core_name);
  147.         exit(1);
  148.     }
  149.     off = USIZE*PAGE_SIZE;
  150.     size = u.u_dsize *PAGE_SIZE;
  151. #endif
  152. #ifdef i386
  153.     if (fread(&c, sizeof(struct core), 1, cfp) != 1)
  154.         Perror("core read");
  155.     if ((c.c_aouthdr.a_text - c.c_aouthdr.a_trsize)!= aout.tsize ||
  156.         c.c_aouthdr.a_data != aout.dsize ||
  157.         c.c_aouthdr.a_bss != aout.bsize) {
  158.         fprintf("mismatch between %s and %s sizes\n", a_out_name, core_name);
  159.         exit(1);}
  160.     aout.dsize = c.c_dsize;
  161.  
  162.     off = 0;
  163.     size = c.c_dsize;
  164. #endif
  165.  
  166.     fh.f_flags |= F_RELFLG | F_EXEC;
  167.     aout.dsize = size;
  168.     aout.bsize = 0;
  169.     tsc.s_size = aout.tsize;
  170.     tsc.s_scnptr = sizeof(fh) + sizeof(aout);
  171.     tsc.s_scnptr += fh.f_nscns * sizeof (struct scnhdr);
  172.     text_scnptr = tsc.s_scnptr;
  173.     lnnoptr = tsc.s_lnnoptr;
  174.     symlocptr = fh.f_symptr;
  175.  
  176.     dsc.s_paddr = dsc.s_vaddr = aout.data_start;
  177.     dsc.s_size = aout.dsize;
  178.     dsc.s_scnptr = tsc.s_scnptr + tsc.s_size;
  179.     data_scnptr = dsc.s_scnptr;
  180.  
  181.     bsc.s_paddr = bsc.s_vaddr = aout.data_start + aout.dsize;
  182.     bsc.s_size = aout.bsize;
  183.     bsc.s_scnptr = 0L;
  184.     bias = dsc.s_scnptr + dsc.s_size - lnnoptr;
  185.  
  186.     if (fh.f_symptr > 0L)
  187.         fh.f_symptr += bias;
  188.     if (tsc.s_lnnoptr > 0L)
  189.         tsc.s_lnnoptr += bias;
  190.  
  191.     if (fwrite(&fh, sizeof(fh), 1, nfp) != 1)
  192.         Perror("fh write");
  193.     if (fwrite(&aout, sizeof(aout), 1, nfp) != 1)
  194.         Perror("aout write");
  195.     if (fwrite(&tsc, sizeof(tsc), 1, nfp) != 1)
  196.         Perror("ts write");
  197.     if (fwrite(&dsc, sizeof(dsc), 1, nfp) != 1)
  198.         Perror("ds write");
  199.     if (fwrite(&bsc, sizeof(bsc), 1, nfp) != 1)
  200.         Perror("bs write");
  201.     fseek(nfp, (long)text_scnptr, 0);
  202.     copy(afp, nfp, aout.tsize);
  203. #ifndef i386
  204.     fseek(cfp, off, 0);
  205. #endif
  206.     fseek(nfp, (long)data_scnptr, 0);
  207.     copy(cfp, nfp, size);
  208.     copy_syms(afp, nfp);
  209.     fclose(nfp);
  210.     fclose(afp);
  211.     fclose(cfp);
  212.     mark_x(new_name);
  213.     exit(0);
  214. }
  215.  
  216. copy_syms(afp, nfp)
  217.     register FILE *afp, *nfp;
  218. {
  219.     char page[BUFSIZ];
  220.     register int n;
  221.     register int nsyms;
  222.     struct syment symentry;
  223.     AUXENT auxentry;
  224.  
  225.     /* if there are line numbers, copy them */
  226.     if (lnnoptr) {
  227.         if (fseek(afp, lnnoptr, 0) == -1L)
  228.             Perror("ln fseek");
  229.         copy(afp, nfp, symlocptr - lnnoptr);
  230.     }
  231.  
  232.     /* now write the symbol table */
  233.     if (fseek(nfp, fh.f_symptr, 0) == -1L)
  234.         Perror("fh fseek");
  235.     for (nsyms = 0; nsyms < fh.f_nsyms; nsyms++) {
  236.         if (fread(&symentry, SYMESZ, 1, afp) != 1)
  237.             Perror("sym fread");
  238.         if (fwrite(&symentry, SYMESZ, 1, nfp) != 1)
  239.             Perror("sym fwrite");
  240.         /*
  241.          * adjust relative offsets of line numbers for
  242.          * function definitions
  243.          */
  244.         if (symentry.n_numaux) {
  245.             if (fread(&auxentry, AUXESZ, 1, afp) != 1)
  246.                 Perror("aux fread");
  247.             nsyms++;
  248.             if (ISFCN (symentry.n_type))
  249.                 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
  250.             if (fwrite(&auxentry, AUXESZ, 1, nfp) != 1)
  251.                 Perror("aux fwrite");
  252.         }
  253.     }
  254.  
  255.     /* finally write the string table, if any */
  256.     while ((n = fread(page, 1, sizeof page, afp)) > 0) {
  257.         if (fwrite(page, 1, n, nfp) != n)
  258.             Perror("sym write");
  259.     }
  260.     if (n < 0)
  261.         Perror("sym read");
  262. }
  263.  
  264. /*
  265.  * After succesfully building the new a.out, mark it executable
  266.  */
  267. mark_x(name)
  268. char *name;
  269. {
  270.     struct stat sbuf;
  271.     int um;
  272.  
  273.     um = umask(777);
  274.     umask(um);
  275.     if (stat(name, &sbuf) == -1)
  276.     {
  277.     perror ("Can't stat new a.out");
  278.     fprintf(stderr, "Setting protection to %o\n", 0777 & ~um);
  279.     sbuf.st_mode = 0777;
  280.     }
  281.     sbuf.st_mode |= 0111 & ~um;
  282.     if (chmod(name, sbuf.st_mode) == -1)
  283.     perror("Couldn't change mode of new a.out to executable");
  284.  
  285. }
  286.  
  287. copy(a, b, size)
  288.     register FILE *a, *b;
  289.     long size;
  290. {
  291.     char buf[BUFSIZ];
  292.     register int i, n;
  293.  
  294.     while (size > 0) {
  295.         i = size;
  296.         if (i > sizeof buf)
  297.             i = sizeof buf;
  298.         if ((n = fread(buf, 1, i, a)) <= 0)
  299.             Perror("copy read");
  300.         if (fwrite(buf, 1, n, b) != n)
  301.             Perror("copy write");
  302.         size -= n;
  303.     }
  304. }
  305.  
  306. Perror(s)
  307.     char *s;
  308. {
  309.     perror(s);
  310.     exit(1);
  311. }
  312. SHAR_EOF
  313. fi
  314. exit 0
  315. #    End of shell archive
  316.