Main Page   Compound List   File List   Compound Members   File Members  

disasm.c

Go to the documentation of this file.
00001 /*
00002  Copyright (c) 1992-1993 The Regents of the University of California.
00003  All rights reserved.  See copyright.h for copyright notice and limitation 
00004  of liability and disclaimer of warranty provisions.
00005  */
00006 
00007 #include "copyright.h"
00008 
00009 /* MIPS instruction disassembler */
00010 
00011 #include <stdio.h>
00012 #include <filehdr.h>
00013 #include <scnhdr.h>
00014 #include <syms.h>
00015 #include <ldfcn.h>
00016 #include "int.h"
00017 
00018 static FILE *fp;
00019 static LDFILE *ldptr;
00020 static SCNHDR texthead, rdatahead, datahead, sdatahead, sbsshead, bsshead;
00021 
00022 static char filename[1000] = "a.out";   /* default a.out file */
00023 static char self[256];                  /* name of invoking program */
00024 
00025 char mem[MEMSIZE];              /* main memory. use malloc later */
00026 int TRACE, Traptrace, Regtrace;
00027 int NROWS=64, ASSOC=1, LINESIZE=4, RAND=0, LRD=0;
00028 int pc;
00029 
00030 extern char *strcpy();
00031 
00032 main(argc, argv)
00033 int argc;
00034 char *argv[];
00035 {
00036         register char *s;
00037         char *fakeargv[3];
00038 
00039         strcpy(self, argv[0]);
00040         while  ( argc > 1  &&  argv[1][0] == '-' )
00041         {
00042                 --argc; ++argv;
00043                 for  ( s=argv[0]+1; *s != '\0'; ++s )
00044                         switch  ( *s )
00045                         {
00046                         }
00047         }
00048 
00049         if (argc >= 2)
00050                 strcpy(filename, argv[1]);
00051         fp = fopen(filename, "r");
00052         if (fp == NULL)
00053         {
00054                 fprintf(stderr, "%s: Could not open '%s'\n", self, filename);
00055                 exit(0);
00056         }
00057         fclose(fp);
00058         load_program(filename);
00059         if  ( argv[1] == NULL )
00060         {
00061                 fakeargv[1] = "a.out";
00062                 fakeargv[2] = NULL;
00063                 argv = fakeargv;
00064                 ++argc;
00065         }
00066         disasm(memoffset, argc-1, argv+1); /* where things normally start */
00067 }
00068 
00069 #define LOADSECTION(head) load_section(&head);
00070 
00071 load_section(hd)
00072 register SCNHDR *hd;
00073 {
00074   register int pc, i;
00075   if  ( hd->s_scnptr != 0 ) {
00076     /* printf("loading %s\n", hd->s_name); */
00077     pc = hd->s_vaddr;
00078     FSEEK(ldptr, hd->s_scnptr, 0);
00079     for  ( i=0; i<hd->s_size; ++i ) {
00080       if (pc-memoffset >= MEMSIZE)
00081         { printf("MEMSIZE too small. Fix and recompile.\n");
00082           exit(1); }
00083       *(char *) ((mem-memoffset)+pc++) = getc(fp);
00084     }
00085   }
00086 }
00087 
00088 load_program(filename)
00089 char *filename;
00090 {
00091         ldptr = ldopen(filename, NULL);
00092         if  ( ldptr == NULL )
00093         {
00094                 fprintf(stderr, "%s: Load read error on %s\n", self, filename);
00095                 exit(0);
00096         }
00097         if  ( TYPE(ldptr) != 0x162 )
00098         {
00099                 fprintf(stderr,
00100                         "big-endian object file (little-endian interp)\n");
00101                 exit(0);
00102         }
00103 
00104         if  ( ldnshread(ldptr, ".text", &texthead) != 1 )
00105                 printf("text section header missing\n");
00106         else
00107                 LOADSECTION(texthead)
00108 
00109         if  ( ldnshread(ldptr, ".rdata", &rdatahead) != 1 )
00110                 printf("rdata section header missing\n");
00111         else
00112                 LOADSECTION(rdatahead)
00113 
00114         if  ( ldnshread(ldptr, ".data", &datahead) != 1 )
00115                 printf("data section header missing\n");
00116         else
00117                 LOADSECTION(datahead)
00118 
00119         if  ( ldnshread(ldptr, ".sdata", &sdatahead) != 1 )
00120                 printf("sdata section header missing\n");
00121         else
00122                 LOADSECTION(sdatahead)
00123 
00124         if  ( ldnshread(ldptr, ".sbss", &sbsshead) != 1 )
00125                 printf("sbss section header missing\n");
00126         else
00127                 LOADSECTION(sbsshead)
00128 
00129         if  ( ldnshread(ldptr, ".bss", &bsshead) != 1 )
00130                 printf("bss section header missing\n");
00131         else
00132                 LOADSECTION(bsshead)
00133 
00134 
00135         /* BSS is already zeroed (statically-allocated mem) */
00136         /* this version ignores relocation info */
00137 }
00138 
00139 
00140 int *m_alloc(n)
00141 int n;
00142 {
00143         extern char *malloc();
00144 
00145         return (int *) (int) malloc((unsigned) n);
00146 }
00147 
00148 disasm(startpc, argc, argv)
00149 int startpc, argc;
00150 char *argv[];
00151 {
00152         int i;
00153 
00154         pc = memoffset;
00155         for  ( i=0; i<texthead.s_size; i += 4 ) 
00156         {
00157                 dis1(pc);
00158                 pc = pc + 4;
00159         }
00160 }
00161 
00162 dis1(xpc)
00163 int xpc;
00164 {
00165         register int instr;
00166 
00167         instr = fetch(pc);
00168         dump_ascii(instr, pc);
00169         printf("\n");
00170 }
00171 
00172 

Generated on Mon Feb 10 09:54:44 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees