/*======================================================================* loadELFFile *======================================================================*/ void loadELF(){ /* 0x1000存放加载的文件数 * 0x1004开始存放文件名,单个字节的 * 0x50000开始,每0x2000个字节放一个bin文件,bin文件的入口为0x400000(4M) */ int i,startAddr; char filename[12]="a.bin"; u8 data[8192]; FILEINFO file; int totalFileAddr=0x1000; u8 totalFile=read_mem_byte(totalFileAddr); for (i=0,startAddr=0x50000; i<totalFile;i++,startAddr+=0x2000){ memset((void *)0x400000+0x2000*i,0,sizeof(data)); memset(data,0,sizeof(data)); readelf(startAddr); memcpy(data,(void *)0x400000+0x2000*i,sizeof(data)); memcpy(file.name,filename,sizeof(filename)); u8 c=read_mem_byte(0x1004+i); if ('A'<=c && c<='Z') c=c-'A'+'a'; file.name[0]=c; file.size=sizeof(data); file.start_pos=new_space(FILESTOREADDR,file.size); write_fileinfo(file); write_data(file.start_pos,data,sizeof(data)); ++filename[0]; } }
int main(int argc, char* argv[], char* envp[]) { int fd; void *map_start; /* will point to the start of the memory maped file */ struct stat fd_stat; /* this is needed to the size of the file */ int count_main = 0; Symlists* symbols = malloc(sizeof(Symlists)); int file; for (file = 1; file<argc; file++) { printf("Checking the file : %s\n", argv[file]); if( (fd = open(argv[file], 2)) < 0 ) { perror("error in open"); exit(-1); } if( fstat(fd, &fd_stat) != 0 ) { perror("stat failed"); exit(-1); } if ( (map_start = mmap(0, fd_stat.st_size, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0)) == MAP_FAILED ) { perror("mmap failed"); exit(-4); } /* now, the file is mapped starting at map_start. */ readelf(map_start, symbols, &count_main); /* now, we unmap the file */ munmap(map_start, fd_stat.st_size); } printf("Final Results :\n"); if (count_main != 1) { printf("main check: FAILED\n"); exit(-1); } printf("main check: PASSED\n"); foreach(symbols->Symlist, check_count); /* Task 3b */ printf("duplicate check: PASSED\n"); check_define(symbols->UnSymlist, symbols->Symlist); /* Task 3c */ printf("no missing symbol : PASSED\n"); return 0; }
/* Maps the input file into memory and runs the initial examination of * the ELF structures. */ static int readinputfile(void) { struct stat stat; void const *ptr; int fd; setfilename(inputfilename); fd = open(inputfilename, O_RDONLY); if (fd < 0) fail(NULL); if (fstat(fd, &stat)) fail(NULL); if (!S_ISREG(stat.st_mode)) fail("not a valid input file."); ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (ptr == MAP_FAILED) fail(NULL); close(fd); return readelf(ptr, stat.st_size); }
int main(int argc, char **argv) { /* The Global MIPS State */ MIPS_state_t mips_state; rs232in_fd = rs232out_fd = -1; char *serial_input_file = NULL; char *serial_output_file = NULL; for (;;) { int c; int option_index = 0; c = getopt_long(argc, argv, "tf:i:o:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: break; case 'f': printf("file %s\n", filename = optarg); break; case 'i': serial_input_file = optarg; break; case 'o': serial_output_file = optarg; break; case '?': usage(argv[0]); break; case 1000: icache_way_lines_log2 = atoi(optarg); break; case 1001: icache_words_in_line_log2 = atoi(optarg); break; case 1002: dcache_way_lines_log2 = atoi(optarg); break; case 1003: dcache_words_in_line_log2 = atoi(optarg); break; default: printf ("?? getopt returned character code 0%o ??\n", c); } } if (optind >= argc) { usage(argv[0]); } while (optind < argc) { readelf(argv[optind++]); } int is_bidir = serial_output_file && serial_input_file && strcmp(serial_input_file, serial_output_file) == 0; if (serial_input_file) { printf("serial input %s\n", serial_input_file); rs232in_fd = open(serial_input_file, (is_bidir ? O_RDWR : O_RDONLY) | O_NONBLOCK); if (rs232in_fd < 0) perror(optarg), exit(1); { /* Turn off echo */ struct termios t; if (tcgetattr(rs232in_fd, &t)) /*perror("getattr")*/; else { t.c_lflag &= ~(ECHO|ECHOE|ECHOK); if (tcsetattr(rs232in_fd, TCSANOW, &t)) perror("setattr"); } } } if (serial_output_file && !is_bidir) { printf("serial output %s\n", serial_output_file); rs232out_fd = open(serial_output_file, O_WRONLY | O_NONBLOCK); if (rs232out_fd < 0) perror(optarg), exit(1); } if (is_bidir) rs232out_fd = rs232in_fd; gettimeofday(&stat_start_time, NULL); switch (run) { case '1': if (enable_graphics) start_sdl(); atexit(print_stats); signal(SIGINT, exit); mips_state.pc = program_entry; init_reg_use_map(); if (screen) { SDL_CreateThread((int (*)(void *))run_simple, &mips_state); mainloop(); } else run_simple(&mips_state); break; case 'b': case 'r': case 'd': case 'm': dump_cache_init_files(); break; case 't': dump_tinymon(); exit(0); default: printf("No XX-run option given\n"); exit(1); } if (enable_testcases) { printf("Test "); if (mips_state.r[7] == 0x1729) { printf("SUCCEED!\n"); exit(0); } else { printf("FAILED! r7 = 0x%08x != 0x1729\n", mips_state.r[7]); exit(1); } } exit(0); }