home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / finger / part03 / read_vmunix.c < prev    next >
C/C++ Source or Header  |  1992-04-03  |  6KB  |  213 lines

  1. /*
  2.  * read_vmunix.c -- read data from an a.out file (for kernel _version)
  3.  *    This is just awful!!
  4.  *
  5.  * Copyright (C) 1986, 1990  Philip L. Budne
  6.  *
  7.  * This file is part of "Phil's Finger Program".
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 1, or (at your option)
  12.  * any later version.
  13.  *
  14.  */
  15.  
  16. # ifndef lint
  17. static char *rcsid = "$Id: read_vmunix.c,v 3.0 90/07/06 13:11:37 budd Rel $";
  18. # endif /* lint not defined */
  19.  
  20. # include "finger.h"
  21. # if Umax != 42 && !defined(USG)
  22.  
  23. # include "args.h"
  24. # ifndef COFF                /* good old a.out */
  25.  
  26. # include <a.out.h>
  27.  
  28. /*
  29.  *    N_SEGSIZ is only needed if the kernel is not an OMAGIC file.
  30.  */
  31.  
  32. # ifdef sun
  33.  
  34. # define LMASK 0xfff            /* crock, seems to work tho */
  35.                     /* for Sun2,3,4 */
  36. /*# define LOADADDR 0xff004000        /* sun4 */
  37. /*# define LOADADDR 0x0f004000        /* sun3 */
  38. /*# define LOADADDR     0x4000        /* sun2 */
  39. # endif /* sun defined */
  40.  
  41. # ifdef accel
  42. # define LMASK 0xffff
  43. # define INCLUDE_HEADERS
  44. # endif /* accel defined */
  45.  
  46. # ifndef N_SEGSIZ            /* a sun-ism */
  47. # include <sys/param.h>            /* get CLBYTES */
  48. # define N_SEGSIZ(a) (CLBYTES)        /* simulate sun macro */
  49. # endif /* N_SEGSIZ not defined */
  50.  
  51. # ifdef vax
  52. # define LOADADDR 0x80000000        /* kernel load address */
  53. # endif /* vax defined */
  54.  
  55. # ifdef ibm032                /* RT under AOS/ACIS 4.3 */
  56. # define LOADADDR 0xe0000000        /* kernel load address */
  57. # endif /* ibm032 defined */
  58.  
  59. # ifdef sony_news            /* only tested for News-800 */
  60. # define LOADADDR 0x80001000        /* crocked by 4k? */
  61. # endif /* sony_news defined */
  62.  
  63. # ifdef sequent                /* only tested for S3 */
  64. # define LOADADDR 0
  65. # endif /* sequent defined */
  66.  
  67. GLOBAL BOOL
  68. read_vmunix( file, off, str, len )
  69. char *file;                /* file name */
  70. long off;                /* virtual address */
  71. char *str;                /* dest buffer */
  72. int len;                /* chars to read */
  73. {
  74.     int f;
  75.     long pos, base, data, koffset;
  76.     struct exec ex;
  77.  
  78.     if( off == 0L )            /* bogus offset */
  79.     return( FALSE );        /* lose quickly */
  80.  
  81.     if( (f = open( file, 0 )) < 0 )    /* open /vmunix */
  82.     return( FALSE );        /* you lose */
  83.  
  84.     if( read( f, &ex, sizeof( ex ) ) != sizeof( ex ) ) { /* read header */
  85.     close( f );            /* short read */
  86.     return( FALSE );        /* quit */
  87.     }
  88.  
  89.     base = N_TXTOFF( ex );        /* get start of meaningfullness */
  90.  
  91. # ifdef LOADADDR
  92.     koffset = LOADADDR;            /* offset into image */
  93. # else  /* LOADADDR not defined */
  94. # ifdef LMASK
  95.     koffset = ex.a_entry & ~LMASK;
  96. # else  /* LMASK not defined */
  97. # include <ERROR: must have one of LOADADDR and LMASK>
  98. # endif /* LMASK not defined */
  99. # endif /* LOADADDR not defined */
  100.  
  101.     off -= koffset;            /* remove kernel offset */
  102.     if( ex.a_magic == OMAGIC
  103. # ifdef NMAGIC                /* sequent lacks NMAGIC! */
  104.        || (off < ex.a_text && ex.a_magic == NMAGIC)
  105. # endif /* NMAGIC defined */
  106. # ifdef SMAGIC                /* sequent standalone */
  107.     || ex.a_magic == SMAGIC
  108. # endif /* SMAGIC defined */
  109.     ) /* OMAGIC or NMAGIC text */
  110.     data = 0;            /* no data offset */
  111. # ifdef NMAGIC
  112.     else if( ex.a_magic == NMAGIC ) {    /* NMAGIC data loaded on seg boundry */
  113.     int segs;            /* segment size */
  114.     segs = N_SEGSIZ(ex) - 1;    /* round to "segment" after text*/
  115.     data = (ex.a_text + segs - 1) & ~segs;
  116.     data -= ex.a_text;        /* just get needed offset */
  117.     }
  118. # endif /* NMAGIC defined */
  119. # ifdef SOME_DAY_OVER_THE_RAINBOW
  120.     else if( ex.a_magic == ZMAGIC ) {    /* never seen a ZMAGIC kernel!! */
  121.     base = N_SEGSIZ(ex);        /* text starts on page boundry */
  122.     if( off >= ex.a_text ) {
  123.         /* something like for NMAGIC above... */
  124.     }
  125.     } /* ZMAGIC */
  126. # endif /* SOME_DAY_OVER_THE_RAINBOW defined */
  127.     else {
  128. # ifdef DEBUGSW
  129.      if( sw_debug )
  130.         printf("Unknown %s magic %#x %#o\n", KERNEL_FILE,
  131.         ex.a_magic, ex.a_magic );
  132. # endif /* DEBUGSW defined */
  133.     return( FALSE );        /* (bad magic falls here too) */
  134.     }
  135.  
  136.     pos = off + base - data;
  137. # ifdef DEBUGSW
  138.     if( sw_debug )
  139.     printf("read_vmunix: pos %#x off %#x base %#x data %#x\n",
  140.            pos, off, base, data );
  141. # endif /* DEBUGSW defined */
  142.  
  143.     if( lseek( f, pos, 0 ) == pos &&    /* seek to position */
  144.        read( f, str, len ) == len ) {    /* and read bytes */
  145.     close( f );            /* ok!! */
  146.     return( TRUE );            /* return good status */
  147.     } /* lseek and read OK */
  148.     close( f );
  149.     return( FALSE );
  150. } /* read_vmunix */
  151.  
  152. # else  /* COFF defined */
  153.  
  154. /*
  155.  *    BSD systems using COFF end up here
  156.  *    ie; DECstation3100 and Sun 386i
  157.  *
  158.  *    *TODO* needs work!
  159.  */
  160.  
  161. # include <stdio.h>
  162. # include <filehdr.h>
  163. # include <syms.h>            /* works for DS3100. 386i?? */
  164. # include <ldfcn.h>
  165.  
  166. # ifndef ISCOFF
  167. /* UGH! This happens only on Umax 4.3 */
  168. # define ISCOFF(m) ((m) >= 0500 && (m) <= 0577)
  169. # endif /* ISCOFF not defined */
  170.  
  171. GLOBAL BOOL
  172. read_vmunix( file, off, str, len )    /* Hacking cough... */
  173.     char *file;                /* file name */
  174.     long off;                /* virtual address */
  175.     char *str;                /* dest buffer */
  176.     int len;                /* chars to read */
  177. {
  178.     LDFILE *ld;
  179.  
  180. # ifdef DEBUGSW
  181.     if( sw_debug )
  182.         printf("read_vmunix('%s', %x, ... , %d)\n", file, off, len );
  183. # endif /* DEBUGSW defined */
  184.  
  185.     if( (ld = ldopen(file, NULL)) == NULL )
  186.     return( FALSE );
  187.  
  188.     for( ; ; ) {            /* bogus loop */
  189.     if( !ISCOFF( HEADER(ld).f_magic ) )
  190.         break;
  191.  
  192. # ifdef DEBUGSW
  193.     if( sw_debug )
  194.         puts("got here");
  195. # endif /* DEBUGSW defined */
  196.  
  197.     /* more good stuff here! */
  198.  
  199.     break;                /* break bogus loop! */
  200.     } /* bogus for loop */
  201.     ldclose( ld );
  202.     return( FALSE );
  203. } /* read_vmunix */
  204. # endif /* COFF defined */
  205.  
  206. # endif /* Umax != 42 && !defined(USG) */
  207.  
  208. /*
  209.  * Local variables:
  210.  * comment-column: 40
  211.  * End:
  212.  */
  213.