Main Page   Compound List   File List   Compound Members   File Members  

out.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 #define MAIN
00008 #include "copyright.h"
00009 #undef MAIN
00010 
00011 /*
00012  * OUT.C
00013  * Looking at a.out formats.
00014  *
00015  * First task:
00016  * Look at mips COFF stuff:
00017  * Print out the contents of a file and do the following:
00018  *    For data, print the value and give relocation information
00019  *    For code, disassemble and give relocation information
00020  */
00021 
00022 #include <filehdr.h>
00023 #include <aouthdr.h>
00024 #include <scnhdr.h>
00025 #include <reloc.h>
00026 #include <syms.h>
00027 #include <stdio.h>
00028 
00029 #define read_struct(f,s) (fread(&s,sizeof(s),1,f)==1)
00030 
00031 #define MAXRELOCS 1000
00032 
00033 
00034 #define MAXDATA 10000
00035 
00036 struct data {
00037   long data[MAXDATA];
00038   struct reloc reloc[MAXRELOCS];
00039   int length;
00040   int relocs;
00041 };
00042 
00043 #define MAXSCNS 10
00044 #define MAXSYMS 300
00045 #define MAXSSPACE 20000
00046 
00047 struct filehdr filehdr;
00048 struct aouthdr aouthdr;
00049 struct scnhdr scnhdr[MAXSCNS];
00050 struct data section[MAXSCNS];
00051 HDRR symhdr;
00052 EXTR symbols[MAXSYMS];
00053 char sspace[20000];
00054 
00055 char *symbol_type[] = {
00056   "Nil", "Global", "Static", "Param", "Local", "Label", "Proc", "Block",
00057   "End", "Member", "Type", "File", "Register", "Forward", "StaticProc",
00058   "Constant" };
00059 
00060 char *storage_class[] = {
00061   "Nil", "Text", "Data", "Bss", "Register", "Abs", "Undefined", "CdbLocal",
00062   "Bits", "CdbSystem", "RegImage", "Info", "UserStruct", "SData", "SBss",
00063   "RData", "Var", "Common", "SCommon", "VarRegister", "Variant", "SUndefined",
00064   "Init" };
00065           
00066 main(argc,argv)
00067 int argc;
00068 char *argv[];
00069 {
00070   char *filename = "a.out";
00071   FILE *f;
00072   int i;
00073   long l;
00074 /*   EXTR filesym; */
00075   char buf[100];
00076 
00077   if (argc == 2) filename = argv[1];
00078   if ((f = fopen(filename,"r")) == NULL) {
00079     printf("out: could not open %s\n",filename);
00080     perror("out");
00081     exit(1);
00082   }
00083   if (!read_struct(f,filehdr) ||
00084       !read_struct(f,aouthdr) ||
00085       filehdr.f_magic != MIPSELMAGIC) {
00086     printf("out: %s is not a MIPS Little-Endian COFF object file\n",filename);
00087     exit(1);
00088   }
00089   if (filehdr.f_nscns > MAXSCNS) {
00090     printf("out: Too many COFF sections.\n");
00091     exit(1);
00092   }
00093   for (i=0; i < filehdr.f_nscns; ++i) {
00094     read_struct(f,scnhdr[i]);
00095     if (scnhdr[i].s_size > MAXDATA*sizeof(long) &&
00096         scnhdr[i].s_scnptr != 0 ||
00097         scnhdr[i].s_nreloc > MAXRELOCS) {
00098       printf("section %s is too big.\n",scnhdr[i].s_name);
00099       exit(1);
00100     }
00101   }
00102   for (i=0; i < filehdr.f_nscns; ++i) {
00103     if (scnhdr[i].s_scnptr != 0) {
00104       section[i].length = scnhdr[i].s_size/4;
00105       fseek(f,scnhdr[i].s_scnptr,0);
00106       fread(section[i].data,sizeof(long),section[i].length,f);
00107       section[i].relocs = scnhdr[i].s_nreloc;
00108       fseek(f,scnhdr[i].s_relptr,0);
00109       fread(section[i].reloc,sizeof(struct reloc),section[i].relocs,f);
00110     } else {
00111       section[i].length = 0;
00112     }
00113   }
00114   fseek(f,filehdr.f_symptr,0);
00115   read_struct(f,symhdr);
00116   if (symhdr.iextMax > MAXSYMS) {
00117     printf("too many symbols to store.\n");
00118   }
00119   fseek(f,symhdr.cbExtOffset,0);
00120   for (i=0; i < MAXSYMS && i<symhdr.iextMax; ++i) {
00121     read_struct(f,symbols[i]);
00122   }
00123   if (symhdr.issExtMax > MAXSSPACE) {
00124     printf("too large a string space.\n");
00125     exit(1);
00126   }
00127   fseek(f,symhdr.cbSsExtOffset,0);
00128   fread(sspace,1,symhdr.issExtMax,f);
00129 
00130   for (i=0; i<filehdr.f_nscns; ++i) {
00131     print_section(i);
00132   }
00133 
00134   printf("External Symbols:\nValue\t Type\t\tStorage Class\tName\n");
00135   for (i=0; i < MAXSYMS && i < symhdr.iextMax; ++i) {
00136     SYMR *sym = &symbols[i].asym;
00137     if (sym->sc == scUndefined) myprintf("\t ");
00138     else myprintf("%08x ",sym->value);
00139     myprintf("%s",symbol_type[sym->st]);
00140     mytab(25);
00141     myprintf("%s",storage_class[sym->sc]);
00142     mytab(41);
00143     myprintf("%s\n",&sspace[sym->iss]);
00144   }
00145   return 0;
00146 }
00147 
00148 static column = 1;
00149 static FILE *outfile = stdout;
00150 
00151 #include <varargs.h>
00152 /*VARARGS0*/
00153 myprintf(va_alist)
00154 va_dcl
00155 {
00156   va_list ap;
00157   char *form;
00158   char buf[100];
00159 
00160   va_start(ap);
00161   form = va_arg(ap,char *);
00162   vsprintf(buf,form,ap);
00163   va_end(ap);
00164 
00165   fputs(buf,outfile);
00166 
00167   for (form = buf; *form != '\0'; ++form) {
00168     if (*form == '\n') column = 1;
00169     else if (*form == '\t') column = ((column + 7)&~7)+1;
00170     else column += 1;
00171   }
00172 }
00173 
00174 mytab(n)
00175 int n;
00176 {
00177   while (column < n) {
00178     fputc(' ',outfile);
00179     ++column;
00180   }
00181   return column == n;
00182 }  
00183 
00184 mysetfile(f)
00185 FILE *f;
00186 {
00187   outfile = f;
00188 }
00189 
00190 #define printf myprintf
00191 #include "d.c"
00192 
00193 print_section(i)
00194 int i;
00195 {
00196   int j,k;
00197   int is_text;
00198   long pc;
00199   long word;
00200   char *s;
00201 
00202   printf("Section: %s\t%d/%d\n",scnhdr[i].s_name,
00203          scnhdr[i].s_size,section[i].relocs);
00204   is_text =  (strncmp(scnhdr[i].s_name,".text",5) == 0);
00205 
00206   for (j=0; j < section[i].length; ++j) {
00207     pc = scnhdr[i].s_vaddr+j*4;
00208     word = section[i].data[j];
00209     if (is_text) {
00210       dump_ascii(word,pc);
00211     } else {
00212       printf("%08x: %08x  ", pc,word);
00213       s = (char *)&word;
00214       for (k=0;k<4;++k) {
00215         if (s[k] >= ' ' && s[k] < 127) printf("%c",s[k]);
00216         else printf(".");
00217       }
00218       printf("\t%d",word);
00219     }
00220     print_reloc(pc,i,j);
00221   }
00222 }
00223 
00224 char *section_name[] = {
00225   "(null)", ".text", ".rdata", ".data", ".sdata", ".sbss", ".bss",
00226   ".init", ".lit8", ".lit4"
00227 };
00228 
00229 char *reloc_type[] = {
00230   "abs", "16", "32", "26", "hi16", "lo16", "gpdata", "gplit"
00231 };
00232 
00233 print_reloc(vaddr,i,j)
00234 int i,j;
00235 {
00236   int k;
00237   struct reloc *rp;
00238   for (k=0; k < section[i].relocs; ++k) {
00239     rp = &section[i].reloc[k];
00240     if (vaddr == rp->r_vaddr) {
00241       mytab(57);
00242       if (rp->r_extern) {
00243         if (rp->r_symndx >= MAXSYMS) {
00244           printf("sym $%d",rp->r_symndx);
00245         } else {
00246           printf("\"%s\"",&sspace[symbols[rp->r_symndx].asym.iss]);
00247         }
00248       } else {
00249         printf("%s",section_name[rp->r_symndx]);
00250       }
00251       printf(" %s",reloc_type[rp->r_type]);
00252       break;
00253     }
00254   }
00255   printf("\n");
00256 }

Generated on Mon Feb 10 09:54:46 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