00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00031 #define StackSize 1024
00032 #define ReadStruct(f,s) Read(f,(char *)&s,sizeof(s))
00033
00034 extern char *malloc();
00035
00036
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
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
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
00069 fdIn = open(argv[1], O_RDONLY, 0);
00070 if (fdIn == -1) {
00071 perror(argv[1]);
00072 exit(1);
00073 }
00074
00075
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
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
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
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
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") &&
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
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 }
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