// AOutExe.cpp: implementation of the CAOutExe class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "AOutExe.h" #include ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// sectionType CAOutExe::m_SectionsForLoading[NUMSECTIONTYPES]= { STYP_TEXT, ".text", STYP_RDATA, ".rdata", STYP_DATA, ".data", STYP_SDATA, ".sdata" }; CAOutExe::CAOutExe() { m_Section=NULL; } CAOutExe::~CAOutExe() { delete[] m_Section; } bool CAOutExe::SendThread() { unsigned int a; int b; bool bDownload; DWORD fileStatus; bool bSuccess; for (a=0; a0) && (strcmp(m_Section[a].name, m_SectionsForLoading[b].name)==0)) { // // one of our flags is set, and the section name // matches one of our 'interesting types' // // therefore download this section // bDownload=true; } } if (bDownload) { // // section pointers are from the beginning of the file // fileStatus=::SetFilePointer(m_hFile, m_Section[a].pointerToData, NULL, FILE_BEGIN); if (fileStatus==0xffffffff) { // // failed // return false; } // // enter binary mode // bSuccess=CTransferrableFile::StartBinaryWrite(*m_pPort); if (!bSuccess) { return false; } // // transfer body of file // bSuccess=CTransferrableFile::TransferDataFromFile(*m_pPort, m_Section[a].physicalAddress, m_Section[a].sectionSize); if (!bSuccess) { return false; } // // leave binary mode // bSuccess=CTransferrableFile::EndBinaryWrite(*m_pPort); if (!bSuccess) { return false; } } } bSuccess=CProgramFile::SetupRegisters(); return bSuccess; } bool CAOutExe::OpenForReading(TCHAR* filename) { fileHeader header; optionalHeader secondHeader; BOOL bReadSuccess; DWORD bytesRead; m_hFile=::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile==INVALID_HANDLE_VALUE) { // // error opening file // return false; } // // read the header // bReadSuccess=::ReadFile(m_hFile, &header, sizeof(fileHeader), &bytesRead, NULL); if (!bReadSuccess) { return false; } if (header.sizeOfOptionalHeader != sizeof(optionalHeader)) { // // file does not have proper header // return false; } if (!(header.flags & F_EXEC)) { // // file is not executable // return false; } // // read the second header // bReadSuccess=::ReadFile(m_hFile, &secondHeader, sizeof(optionalHeader), &bytesRead, NULL); if (!bReadSuccess) { return false; } // // set values of GP, entry point and stack // m_GPValue=secondHeader.gp_value; m_PCValue=secondHeader.entry; m_SPValue=0x801ffff0; // // read in section headers // m_NumSections=header.numSections; m_Section=new sectionHeader[m_NumSections]; if (m_Section==NULL) { return false; } // // read all section headers in one go // bReadSuccess=::ReadFile(m_hFile, m_Section, sizeof(sectionHeader)*m_NumSections, &bytesRead, NULL); if (!bReadSuccess) { return false; } return true; }