Main Page   Compound List   File List   Compound Members   File Members  

coff2flat.c

Go to the documentation of this file.
00001 /*
00002  Copyright (c) 1992 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 /* This program reads in a COFF format file, and outputs a flat file --
00008  * the flat file can then be copied directly to virtual memory and executed.
00009  * In other words, the various pieces of the object code are loaded at 
00010  * the appropriate offset in the flat file.
00011  *
00012  * Assumes coff file compiled with -N -T 0 to make sure it's not shared text.
00013  */
00014 
00015 #define MAIN
00016 #include "copyright.h"
00017 #undef MAIN
00018 
00019 #include <filehdr.h>
00020 #include <aouthdr.h>
00021 #include <scnhdr.h>
00022 #include <reloc.h>
00023 #include <syms.h>
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <fcntl.h>
00027 #include <limits.h>
00028 #include <stdio.h>
00029 
00030 /* NOTE -- once you have implemented large files, it's ok to make this bigger! */
00031 #define StackSize               1024      /* in bytes */
00032 #define ReadStruct(f,s)         Read(f,(char *)&s,sizeof(s))
00033 
00034 extern char *malloc();
00035 
00036 /* read and check for error */
00037 void Read(int fd, char *buf, int nBytes)
00038 {
00039     if (read(fd, buf, nBytes) != nBytes) {
00040         fprintf(stderr, "File is too short\n");
00041         exit(1);
00042     }
00043 }
00044 
00045 /* write and check for error */
00046 void Write(int fd, char *buf, int nBytes)
00047 {
00048     if (write(fd, buf, nBytes) != nBytes) {
00049         fprintf(stderr, "Unable to write file\n");
00050         exit(1);
00051     }
00052 }
00053 
00054 /* do the real work */
00055 main (int argc, char **argv)
00056 {
00057     int fdIn, fdOut, numsections, i, top, tmp;
00058     struct filehdr fileh;
00059     struct aouthdr systemh;
00060     struct scnhdr *sections;
00061     char *buffer;
00062 
00063     if (argc < 2) {
00064         fprintf(stderr, "Usage: %s <coffFileName> <flatFileName>\n", argv[0]);
00065         exit(1);
00066     }
00067     
00068 /* open the object file (input) */
00069     fdIn = open(argv[1], O_RDONLY, 0);
00070     if (fdIn == -1) {
00071         perror(argv[1]);
00072         exit(1);
00073     }
00074 
00075 /* open the flat file (output) */
00076     fdOut = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0666);
00077     if (fdIn == -1) {
00078         perror(argv[2]);
00079         exit(1);
00080     }
00081     
00082 /* Read in the file header and check the magic number. */
00083     ReadStruct(fdIn,fileh);
00084     if (fileh.f_magic != MIPSELMAGIC) {
00085         fprintf(stderr, "File is not a MIPSEL COFF file\n");
00086         exit(1);
00087     }
00088     
00089 /* Read in the system header and check the magic number */
00090     ReadStruct(fdIn,systemh);
00091     if (systemh.magic != OMAGIC) {
00092         fprintf(stderr, "File is not a OMAGIC file\n");
00093         exit(1);
00094     }
00095     
00096 /* Read in the section headers. */
00097     numsections = fileh.f_nscns;
00098     sections = (struct scnhdr *)malloc(fileh.f_nscns * sizeof(struct scnhdr));
00099     Read(fdIn, (char *) sections, fileh.f_nscns * sizeof(struct scnhdr));
00100 
00101  /* Copy the segments in */
00102     printf("Loading %d sections:\n", fileh.f_nscns);
00103     for (top = 0, i = 0; i < fileh.f_nscns; i++) {
00104         printf("\t\"%s\", filepos 0x%x, mempos 0x%x, size 0x%x\n",
00105               sections[i].s_name, sections[i].s_scnptr,
00106               sections[i].s_paddr, sections[i].s_size);
00107         if ((sections[i].s_paddr + sections[i].s_size) > top)
00108             top = sections[i].s_paddr + sections[i].s_size;
00109         if (strcmp(sections[i].s_name, ".bss") && /* no need to copy if .bss */
00110                 strcmp(sections[i].s_name, ".sbss")) {
00111             lseek(fdIn, sections[i].s_scnptr, 0);
00112             buffer = malloc(sections[i].s_size);
00113             Read(fdIn, buffer, sections[i].s_size);
00114             Write(fdOut, buffer, sections[i].s_size);
00115             free(buffer);
00116         }
00117     }
00118 /* put a blank word at the end, so we know where the end is! */
00119     printf("Adding stack of size: %d\n", StackSize);
00120     lseek(fdOut, top + StackSize - 4, 0);
00121     tmp = 0;
00122     Write(fdOut, (char *)&tmp, 4);
00123 
00124     close(fdIn);
00125     close(fdOut);
00126 }

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