Main Page   Compound List   File List   Compound Members   File Members  

coff2flat.c File Reference

#include "copyright.h"
#include <filehdr.h>
#include <aouthdr.h>
#include <scnhdr.h>
#include <reloc.h>
#include <syms.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>

Go to the source code of this file.

Defines

#define MAIN
#define StackSize   1024
#define ReadStruct(f, s)   Read(f,(char *)&s,sizeof(s))

Functions

char * malloc ()
void Read (int fd, char *buf, int nBytes)
void Write (int fd, char *buf, int nBytes)
 main (int argc, char **argv)


Define Documentation

#define MAIN
 

Definition at line 15 of file coff2flat.c.

#define ReadStruct f,
     Read(f,(char *)&s,sizeof(s))
 

Definition at line 32 of file coff2flat.c.

Referenced by main.

#define StackSize   1024
 

Definition at line 31 of file coff2flat.c.

Referenced by main.


Function Documentation

main int    argc,
char **    argv
 

Definition at line 55 of file coff2flat.c.

References close, exit, filehdr::f_magic, filehdr::f_nscns, lseek, aouthdr::magic, malloc, MIPSELMAGIC, OMAGIC, open, Read, ReadStruct, scnhdr::s_paddr, scnhdr::s_size, StackSize, and Write.

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 }

char* malloc  
 

Referenced by main.

void Read int    fd,
char *    buf,
int    nBytes
 

Definition at line 37 of file coff2flat.c.

References exit, and read.

00038 {
00039     if (read(fd, buf, nBytes) != nBytes) {
00040         fprintf(stderr, "File is too short\n");
00041         exit(1);
00042     }
00043 }

void Write int    fd,
char *    buf,
int    nBytes
 

Definition at line 46 of file coff2flat.c.

References exit, and write.

00047 {
00048     if (write(fd, buf, nBytes) != nBytes) {
00049         fprintf(stderr, "Unable to write file\n");
00050         exit(1);
00051     }
00052 }


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