00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #if defined(_MSC_VER) && _MSC_VER <= 1200
00022 #pragma warning(disable:4786)
00023 #endif
00024
00025 #include "ExeBinaryFile.h"
00026
00027 ExeBinaryFile::ExeBinaryFile()
00028 {
00029 }
00030
00031 bool ExeBinaryFile::RealLoad(const char* sName)
00032 {
00033 FILE *fp;
00034 int i, cb;
00035 Byte buf[4];
00036 int fCOM;
00037
00038 m_pFileName = sName;
00039
00040
00041 m_pSections = new SectionInfo[3];
00042 if (m_pSections == 0)
00043 {
00044 fprintf(stderr, "Could not allocate section information\n");
00045 return 0;
00046 }
00047 m_iNumSections = 3;
00048 m_pHeader = new exeHeader;
00049 if (m_pHeader == 0)
00050 {
00051 fprintf(stderr, "Could not allocate header memory\n");
00052 return 0;
00053 }
00054
00055
00056 if ((fp = fopen(sName, "rb")) == NULL)
00057 {
00058 fprintf(stderr, "Could not open file %s\n", sName);
00059 return 0;
00060 }
00061
00062
00063 if (fread(m_pHeader, 1, 2, fp) != 2)
00064 {
00065 fprintf(stderr, "Cannot read file %s\n", sName);
00066 return 0;
00067 }
00068
00069
00070 if (! (fCOM = (m_pHeader->sigLo != 0x4D || m_pHeader->sigHi != 0x5A)))
00071 {
00072
00073 fseek(fp, 0, SEEK_SET);
00074 if (fread(m_pHeader, sizeof(exeHeader), 1, fp) != 1)
00075 {
00076 fprintf(stderr, "Cannot read file %s\n", sName);
00077 return 0;
00078 }
00079
00080
00081 if (LH(&m_pHeader->relocTabOffset) == 0x40)
00082 {
00083 fprintf(stderr, "Error - NE format executable\n");
00084 return 0;
00085 }
00086
00087
00088
00089
00090
00091
00092 cb = (dword)LH(&m_pHeader->numPages) * 512 -
00093 (dword)LH(&m_pHeader->numParaHeader) * 16;
00094 if (m_pHeader->lastPageSize)
00095 {
00096 cb -= 512 - LH(&m_pHeader->lastPageSize);
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 m_cReloc = (SWord)LH(&m_pHeader->numReloc);
00108
00109
00110 if (m_cReloc)
00111 {
00112 m_pRelocTable = new dword[m_cReloc];
00113 if (m_pRelocTable == 0)
00114 {
00115 fprintf(stderr, "Could not allocate relocation table "
00116 "(%d entries)\n", m_cReloc);
00117 return 0;
00118 }
00119 fseek(fp, LH(&m_pHeader->relocTabOffset), SEEK_SET);
00120
00121
00122 for (i = 0; i < m_cReloc; i++)
00123 {
00124 fread(buf, 1, 4, fp);
00125 m_pRelocTable[i] = LH(buf) +
00126 (((int)LH(buf+2))<<4);
00127 }
00128 }
00129
00130
00131 fseek(fp, (int)LH(&m_pHeader->numParaHeader) * 16, SEEK_SET);
00132
00133
00134
00135 m_uInitPC = ((LH(&m_pHeader->initCS)) << 16) + LH(&m_pHeader->initIP);
00136 m_uInitSP = ((LH(&m_pHeader->initSS)) << 16) + LH(&m_pHeader->initSP);
00137 }
00138 else
00139 {
00140
00141
00142 fseek(fp, 0, SEEK_END);
00143 cb = ftell(fp);
00144
00145
00146
00147
00148
00149 m_uInitPC = 0x100;
00150 m_uInitSP = 0xFFFE;
00151 m_cReloc = 0;
00152
00153 fseek(fp, 0, SEEK_SET);
00154 }
00155
00156
00157 m_cbImage = cb;
00158 m_pImage = new Byte[m_cbImage];
00159
00160 if (cb != (int)fread(m_pImage, 1, (size_t)cb, fp))
00161 {
00162 fprintf(stderr, "Cannot read file %s\n", sName);
00163 return 0;
00164 }
00165
00166
00167 if (m_cReloc)
00168 {
00169 for (i = 0; i < m_cReloc; i++)
00170 {
00171 Byte *p = &m_pImage[m_pRelocTable[i]];
00172 SWord w = (SWord)LH(p);
00173 *p++ = (Byte)(w & 0x00FF);
00174 *p = (Byte)((w & 0xFF00) >> 8);
00175 }
00176 }
00177
00178 fclose(fp);
00179
00180 m_pSections[0].pSectionName = "$HEADER";
00181
00182 m_pSections[0].uNativeAddr = 0;
00183 m_pSections[0].uHostAddr = (DWord)m_pHeader;
00184 m_pSections[0].uSectionSize = sizeof(exeHeader);
00185 m_pSections[0].uSectionEntrySize = 1;
00186
00187 m_pSections[1].pSectionName = ".text";
00188 m_pSections[1].bCode = true;
00189 m_pSections[1].bData = true;
00190 m_pSections[1].uNativeAddr = 0;
00191 m_pSections[1].uHostAddr = (DWord)m_pImage;
00192 m_pSections[1].uSectionSize = m_cbImage;
00193 m_pSections[1].uSectionEntrySize = 1;
00194
00195 m_pSections[2].pSectionName = "$RELOC";
00196
00197 m_pSections[2].uNativeAddr = 0;
00198 m_pSections[2].uHostAddr = (DWord)m_pRelocTable;
00199 m_pSections[2].uSectionSize = sizeof(DWord) * m_cReloc;
00200 m_pSections[2].uSectionEntrySize = sizeof(DWord);
00201
00202 return 1;
00203
00204 }
00205
00206
00207 void ExeBinaryFile::UnLoad()
00208 {
00209 if (m_pHeader) delete m_pHeader;
00210 if (m_pImage) delete [] m_pImage;
00211 if (m_pRelocTable) delete [] m_pRelocTable;
00212 }
00213
00214 char* ExeBinaryFile::SymbolByAddr(ADDRESS dwAddr)
00215 {
00216 if (dwAddr == GetMainEntryPoint())
00217 return "main";
00218
00219
00220 return 0;
00221 }
00222
00223 bool ExeBinaryFile::DisplayDetails(const char* fileName, FILE* f
00224 )
00225 {
00226 return false;
00227 }
00228
00229 LOAD_FMT ExeBinaryFile::GetFormat() const
00230 {
00231 return LOADFMT_EXE;
00232 }
00233
00234 MACHINE ExeBinaryFile::GetMachine() const
00235 {
00236 return MACHINE_PENTIUM;
00237 }
00238
00239 bool ExeBinaryFile::Open(const char* sName)
00240 {
00241
00242 return false;
00243 }
00244 void ExeBinaryFile::Close()
00245 {
00246
00247 return;
00248 }
00249 bool ExeBinaryFile::PostLoad(void* handle)
00250 {
00251
00252 return false;
00253 }
00254
00255 bool ExeBinaryFile::isLibrary() const
00256 {
00257 return false;
00258 }
00259
00260 std::list<const char *> ExeBinaryFile::getDependencyList()
00261 {
00262 return std::list<const char *>();
00263 }
00264
00265 ADDRESS ExeBinaryFile::getImageBase()
00266 {
00267 return 0;
00268 }
00269
00270 size_t ExeBinaryFile::getImageSize()
00271 {
00272 return 0;
00273 }
00274
00275
00276 ADDRESS ExeBinaryFile::GetMainEntryPoint()
00277 {
00278 return NO_ADDRESS;
00279 }
00280
00281 ADDRESS ExeBinaryFile::GetEntryPoint()
00282 {
00283
00284 return (ADDRESS)((LH(&m_pHeader->initCS) << 4) + LH(&m_pHeader->initIP));
00285 }
00286
00287
00288 std::list<SectionInfo*>& ExeBinaryFile::GetEntryPoints(const char* pEntry
00289 ) {
00290 std::list<SectionInfo*>* ret = new std::list<SectionInfo*>;
00291 #if 0 // Copied from PalmBinaryFile.cc
00292 SectionInfo* pSect = GetSectionInfoByName("code1");
00293 if (pSect == 0)
00294 return *ret;
00295 ret->push_back(pSect);
00296 #endif
00297 return *ret;
00298 }
00299
00300
00301
00302
00303
00304 extern "C" {
00305 #ifdef _WIN32
00306 __declspec(dllexport)
00307 #endif
00308 BinaryFile* construct()
00309 {
00310 return new ExeBinaryFile;
00311 }
00312 }
00313