| 1 | /* $Id: pe.cpp,v 1.4 1999-06-21 01:15:40 buerkle Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * PE2LX PE image interpreter
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright 1998 Knut St. Osmundsen
|
|---|
| 8 | *
|
|---|
| 9 | *
|
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 11 | *
|
|---|
| 12 | */
|
|---|
| 13 | #define INCL_BITMAPFILEFORMAT
|
|---|
| 14 | #define INCL_DOSFILEMGR /* File Manager values */
|
|---|
| 15 | #define INCL_DOSERRORS /* DOS Error values */
|
|---|
| 16 | #define INCL_DOSPROCESS /* DOS Process values */
|
|---|
| 17 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
|---|
| 18 | #define INCL_WIN
|
|---|
| 19 | #define INCL_BASE
|
|---|
| 20 | #define INCL_GPIBITMAPS
|
|---|
| 21 | #include <os2.h>
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <string.h>
|
|---|
| 24 | #include <stdlib.h>
|
|---|
| 25 | #include <iostream.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <assert.h>
|
|---|
| 28 | #include <versionos2.h>
|
|---|
| 29 | #include <pefile.h>
|
|---|
| 30 | #include <winimage.h>
|
|---|
| 31 | #include "lx.h"
|
|---|
| 32 | #include "menu.h"
|
|---|
| 33 | #include "dialog.h"
|
|---|
| 34 | #include "icon.h"
|
|---|
| 35 | #include "icongrp.h"
|
|---|
| 36 | #include "rcdata.h"
|
|---|
| 37 | #include "misc.h"
|
|---|
| 38 | #include "strings.h"
|
|---|
| 39 | #include "bitmap.h"
|
|---|
| 40 | #include "accelerator.h"
|
|---|
| 41 | #include "cursor.h"
|
|---|
| 42 | #include "cursorgrp.h"
|
|---|
| 43 |
|
|---|
| 44 | //#define FORWARDERS
|
|---|
| 45 |
|
|---|
| 46 | #ifndef max /*PLF Sat 97-06-21 22:25:44*/
|
|---|
| 47 | #define max(a,b) ((a>b) ? (a) : (b))
|
|---|
| 48 | #endif
|
|---|
| 49 |
|
|---|
| 50 | /*heximal(decimal) KSO Sun 24.05.1998*/
|
|---|
| 51 | char szHexBuffer[30];
|
|---|
| 52 |
|
|---|
| 53 | char *hex(ULONG num)
|
|---|
| 54 | {
|
|---|
| 55 | sprintf(szHexBuffer, "0x%+08x (%lu)",num,num);
|
|---|
| 56 | return szHexBuffer;
|
|---|
| 57 | }
|
|---|
| 58 | /**/
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | char INFO_BANNER[] =
|
|---|
| 62 | "Usage: PE2LX winfile \n\
|
|---|
| 63 | OR\n\
|
|---|
| 64 | PE2LX winfile os2file\n";
|
|---|
| 65 |
|
|---|
| 66 | char *ResTypes[MAX_RES] =
|
|---|
| 67 | {"niks", "CURSOR", "BITMAP", "ICON", "MENU", "DIALOG", "STRING",
|
|---|
| 68 | "FONTDIR", "FONT", "ACCELERATOR", "RCDATA", "MESSAGETABLE",
|
|---|
| 69 | "GROUP_CURSOR", "niks", "GROUP_ICON", "niks", "VERSION"};
|
|---|
| 70 |
|
|---|
| 71 | void ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType, int level, PIMAGE_RESOURCE_DIRECTORY prdRoot, int VirtualAddress, int type, int id);
|
|---|
| 72 | void StoreIdResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType, int level, PIMAGE_RESOURCE_DIRECTORY prdRoot, int VirtualAddress, int type, int id);
|
|---|
| 73 | BOOL ProcessImports(void *pFile);
|
|---|
| 74 |
|
|---|
| 75 | int main(int argc, char *argv[])
|
|---|
| 76 | {
|
|---|
| 77 | HFILE win32handle;
|
|---|
| 78 | ULONG ulAction = 0; /* Action taken by DosOpen */
|
|---|
| 79 | ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
|
|---|
| 80 | APIRET rc = NO_ERROR; /* Return code */
|
|---|
| 81 | ULONG filesize, ulRead;
|
|---|
| 82 | LPVOID win32file = NULL;
|
|---|
| 83 | IMAGE_SECTION_HEADER sh;
|
|---|
| 84 | IMAGE_OPTIONAL_HEADER oh;
|
|---|
| 85 | IMAGE_FILE_HEADER fh;
|
|---|
| 86 | PIMAGE_BASE_RELOCATION prel;
|
|---|
| 87 | PIMAGE_RESOURCE_DIRECTORY prdRoot, prdType;
|
|---|
| 88 | PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
|
|---|
| 89 | PIMAGE_RESOURCE_DATA_ENTRY pData;
|
|---|
| 90 | PIMAGE_EXPORT_DIRECTORY ped;
|
|---|
| 91 | char *page;
|
|---|
| 92 | int i, j, count, id;
|
|---|
| 93 | int *ptrNames, *ptrAddress;
|
|---|
| 94 | USHORT *ptrOrd;
|
|---|
| 95 | PIMAGE_SECTION_HEADER psh;
|
|---|
| 96 | int nSections;
|
|---|
| 97 |
|
|---|
| 98 | if(argc != 2 && argc != 3) {
|
|---|
| 99 | cout << "pe2lx v0.0." << PE2LX_VERSION << "alpha"<< endl;
|
|---|
| 100 | cout << INFO_BANNER << endl;
|
|---|
| 101 | return(0);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | rc = DosOpen(argv[1], /* File path name */
|
|---|
| 105 | &win32handle, /* File handle */
|
|---|
| 106 | &ulAction, /* Action taken */
|
|---|
| 107 | 0L, /* File primary allocation */
|
|---|
| 108 | 0L, /* File attribute */
|
|---|
| 109 | OPEN_ACTION_FAIL_IF_NEW |
|
|---|
| 110 | OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
|
|---|
| 111 | OPEN_FLAGS_NOINHERIT |
|
|---|
| 112 | OPEN_SHARE_DENYNONE |
|
|---|
| 113 | OPEN_ACCESS_READONLY, /* Open mode of the file */
|
|---|
| 114 | 0L); /* No extended attribute */
|
|---|
| 115 |
|
|---|
| 116 | if (rc != NO_ERROR) {
|
|---|
| 117 | cout << "DosOpen returned " << rc << endl;
|
|---|
| 118 | return 1;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /* Move the file pointer back to the beginning of the file */
|
|---|
| 122 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
|---|
| 123 | DosSetFilePtr(win32handle, 0L, FILE_END, &filesize);
|
|---|
| 124 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
|---|
| 125 |
|
|---|
| 126 | win32file = malloc(filesize);
|
|---|
| 127 | if(win32file == NULL) {
|
|---|
| 128 | cout << "Error allocating " << filesize << " bytes" << endl;
|
|---|
| 129 | //SvL: Probably a huge installation file where the installation data is
|
|---|
| 130 | // simply appended
|
|---|
| 131 | filesize = 1024*1024;
|
|---|
| 132 |
|
|---|
| 133 | win32file = malloc(filesize);
|
|---|
| 134 | if(win32file == NULL) {
|
|---|
| 135 | DosClose(win32handle); /* Close the file */
|
|---|
| 136 | return(1);
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | rc = DosRead(win32handle, win32file, filesize, &ulRead);
|
|---|
| 140 | if(rc != NO_ERROR) {
|
|---|
| 141 | cout << "DosRead returned " << rc << endl;
|
|---|
| 142 | DosClose(win32handle); /* Close the file */
|
|---|
| 143 | return(1);
|
|---|
| 144 | }
|
|---|
| 145 | DosClose(win32handle); /* Close the file */ /*PLF Wed 98-03-18 02:32:26 moved */
|
|---|
| 146 |
|
|---|
| 147 | if(GetPEFileHeader (win32file, &fh) == FALSE) {
|
|---|
| 148 | cout << "Not a valid PE file (probably a 16 bits windows exe/dll)!" << endl;
|
|---|
| 149 | return(1);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
|
|---|
| 153 | cout << "Not a valid PE file!" << endl;
|
|---|
| 154 | return(1);
|
|---|
| 155 | }
|
|---|
| 156 | if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
|
|---|
| 157 | cout << "You need a REAL CPU to run this code" << endl;
|
|---|
| 158 | return(1);
|
|---|
| 159 | }
|
|---|
| 160 | //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
|
|---|
| 161 | if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
|
|---|
| 162 | cout << "Can't convert system files" << endl;
|
|---|
| 163 | return(1);
|
|---|
| 164 | }
|
|---|
| 165 | if(argc == 2) {
|
|---|
| 166 | //ok, it's a PE file, so we can safely make a backup copy
|
|---|
| 167 | char *newfile = (char *)malloc(strlen(argv[1])+1);
|
|---|
| 168 | strcpy(newfile, argv[1]);
|
|---|
| 169 | newfile[strlen(newfile)-1]++;
|
|---|
| 170 | //save copy of win32 exe/dll (exe->exf, dll->dlk)
|
|---|
| 171 | rc = DosMove(argv[1], newfile);
|
|---|
| 172 | if(rc) {
|
|---|
| 173 | cout << "Unable to save original win32 file to " << newfile << "(" << rc << ")" << endl;
|
|---|
| 174 | free(newfile);
|
|---|
| 175 | return(1);
|
|---|
| 176 | }
|
|---|
| 177 | free(newfile);
|
|---|
| 178 | }
|
|---|
| 179 | if(fh.Characteristics & IMAGE_FILE_DLL)
|
|---|
| 180 | OS2Exe.SetExeType(FALSE);
|
|---|
| 181 | else OS2Exe.SetExeType(TRUE);
|
|---|
| 182 |
|
|---|
| 183 | if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
|
|---|
| 184 | cout << "No fixups, might not run!" << endl;
|
|---|
| 185 | OS2Exe.SetNoFixups();
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | GetPEOptionalHeader (win32file, &oh);
|
|---|
| 189 | cout << "PE Optional header: " << endl;
|
|---|
| 190 | #if 0
|
|---|
| 191 | oh.ImageBase = 0x20000000;
|
|---|
| 192 | #endif
|
|---|
| 193 | cout << "Preferred address : " << hex(oh.ImageBase) << endl;
|
|---|
| 194 | cout << "Base Of Code : " << hex(oh.BaseOfCode) << endl;
|
|---|
| 195 | cout << "CodeSize : " << hex(oh.SizeOfCode) << endl;
|
|---|
| 196 | cout << "Base Of Data : " << hex(oh.BaseOfData) << endl;
|
|---|
| 197 | cout << "Data Size (uninit): " << hex(oh.SizeOfUninitializedData) << endl;
|
|---|
| 198 | cout << "Data Size (init) : " << hex(oh.SizeOfInitializedData) << endl;
|
|---|
| 199 | cout << "Entry Point : " << hex(oh.AddressOfEntryPoint) << endl;
|
|---|
| 200 | cout << "Section Alignment : " << hex(oh.SectionAlignment) << endl;
|
|---|
| 201 | cout << "Stack Reserve size: " << hex(oh.SizeOfStackReserve) << endl;
|
|---|
| 202 | cout << "Stack Commit size : " << hex(oh.SizeOfStackCommit) << endl;
|
|---|
| 203 | cout << "SizeOfHeapReserve : " << hex(oh.SizeOfHeapReserve) << endl;
|
|---|
| 204 | cout << "SizeOfHeapCommit : " << hex(oh.SizeOfHeapCommit) << endl;
|
|---|
| 205 | cout << "FileAlignment : " << hex(oh.FileAlignment) << endl;
|
|---|
| 206 |
|
|---|
| 207 | //SetExeType must have been called before
|
|---|
| 208 | if(oh.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_GUI) {
|
|---|
| 209 | OS2Exe.SetModuleType(SYSTEM_CHARACTER);
|
|---|
| 210 | cout << "Console app" << endl;
|
|---|
| 211 | }
|
|---|
| 212 | else OS2Exe.SetModuleType(SYSTEM_GUI);
|
|---|
| 213 |
|
|---|
| 214 | //// OS2Exe.SetStackSize(oh.SizeOfStackCommit);
|
|---|
| 215 | OS2Exe.SetStackSize(max(oh.SizeOfStackCommit, oh.SizeOfStackReserve));
|
|---|
| 216 | if(argc == 2)
|
|---|
| 217 | OS2Exe.SetModuleName(argv[1]);
|
|---|
| 218 | else OS2Exe.SetModuleName(argv[2]);
|
|---|
| 219 |
|
|---|
| 220 | nSections = NR_SECTIONS(win32file);
|
|---|
| 221 |
|
|---|
| 222 | if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL) {
|
|---|
| 223 | cout << endl;
|
|---|
| 224 | for (i=0; i<nSections; i++) {
|
|---|
| 225 | cout << "Section Name: " << psh[i].Name << endl;
|
|---|
| 226 | cout << "Raw data size: " << hex(psh[i].SizeOfRawData) << endl;
|
|---|
| 227 | cout << "Virtual Address: " << hex(psh[i].VirtualAddress) << endl;
|
|---|
| 228 | cout << "Virtual Size: " << hex(psh[i].Misc.VirtualSize) << endl;
|
|---|
| 229 | cout << "Pointer to raw data: " << hex(psh[i].PointerToRawData) << endl;
|
|---|
| 230 | cout << "Section flags: " << hex(psh[i].Characteristics) << endl;
|
|---|
| 231 | if(strcmp(psh[i].Name, ".reloc") == 0) {
|
|---|
| 232 | cout << ".reloc" << endl << endl;
|
|---|
| 233 | continue;
|
|---|
| 234 | }
|
|---|
| 235 | if(strcmp(psh[i].Name, ".edata") == 0) {
|
|---|
| 236 | cout << ".edata" << endl << endl;
|
|---|
| 237 | continue;
|
|---|
| 238 | }
|
|---|
| 239 | if(strcmp(psh[i].Name, ".pdata") == 0) {
|
|---|
| 240 | cout << ".pdata" << endl << endl;
|
|---|
| 241 | continue;
|
|---|
| 242 | }
|
|---|
| 243 | if(strcmp(psh[i].Name, ".rsrc") == 0) {
|
|---|
| 244 | cout << ".rsrc" << endl << endl;
|
|---|
| 245 | continue;
|
|---|
| 246 | }
|
|---|
| 247 | if(strcmp(psh[i].Name, ".debug") == 0) {
|
|---|
| 248 | cout << ".rdebug" << endl << endl;
|
|---|
| 249 | continue;
|
|---|
| 250 | }
|
|---|
| 251 | if(IsImportSection(win32file, &psh[i]))
|
|---|
| 252 | {
|
|---|
| 253 | int type = SECTION_IMPORT;
|
|---|
| 254 | cout << "Import Data Section" << endl << endl;
|
|---|
| 255 | if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
|
|---|
| 256 | cout << "Also Code Section" << endl << endl;
|
|---|
| 257 | OS2Exe.SetEntryAddress(oh.AddressOfEntryPoint-psh[i].VirtualAddress);
|
|---|
| 258 | type |= SECTION_CODE;
|
|---|
| 259 | }
|
|---|
| 260 | OS2Exe.StoreSection((char *)win32file+psh[i].PointerToRawData,
|
|---|
| 261 | psh[i].SizeOfRawData, psh[i].Misc.VirtualSize,
|
|---|
| 262 | psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 263 | type);
|
|---|
| 264 | continue;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | if ((psh[i].Characteristics & IMAGE_SCN_CNT_CODE)
|
|---|
| 268 | //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section (TIB fix need to have the first section marked as code)
|
|---|
| 269 | || (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
|
|---|
| 270 | !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA)) //KSO: make sure its not marked as a datasection
|
|---|
| 271 | )
|
|---|
| 272 | ) {
|
|---|
| 273 | cout << "Code Section" << endl << endl;
|
|---|
| 274 | if(oh.AddressOfEntryPoint < psh[i].VirtualAddress)
|
|---|
| 275 | OS2Exe.SetEntryAddress(oh.AddressOfEntryPoint - oh.BaseOfCode);
|
|---|
| 276 | else OS2Exe.SetEntryAddress(oh.AddressOfEntryPoint-psh[i].VirtualAddress);
|
|---|
| 277 | OS2Exe.StoreSection((char *)win32file+psh[i].PointerToRawData,
|
|---|
| 278 | psh[i].SizeOfRawData, psh[i].Misc.VirtualSize,
|
|---|
| 279 | psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 280 | SECTION_CODE);
|
|---|
| 281 | continue;
|
|---|
| 282 | }
|
|---|
| 283 | if(!(psh[i].Characteristics & IMAGE_SCN_MEM_WRITE)) { //read only data section
|
|---|
| 284 | cout << "Read Only Data Section" << endl << endl;
|
|---|
| 285 | OS2Exe.StoreSection((char *)win32file+psh[i].PointerToRawData,
|
|---|
| 286 | psh[i].SizeOfRawData, psh[i].Misc.VirtualSize,
|
|---|
| 287 | psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 288 | SECTION_READONLYDATA);
|
|---|
| 289 | continue;
|
|---|
| 290 | }
|
|---|
| 291 | if(psh[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
|
|---|
| 292 | cout << "Uninitialized Data Section" << endl << endl;
|
|---|
| 293 | OS2Exe.StoreSection((char *)win32file+psh[i].PointerToRawData,
|
|---|
| 294 | psh[i].SizeOfRawData, psh[i].Misc.VirtualSize,
|
|---|
| 295 | psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 296 | SECTION_UNINITDATA);
|
|---|
| 297 | continue;
|
|---|
| 298 | }
|
|---|
| 299 | if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
|
|---|
| 300 | cout << "Initialized Data Section" << endl << endl;
|
|---|
| 301 | OS2Exe.StoreSection((char *)win32file+psh[i].PointerToRawData,
|
|---|
| 302 | psh[i].SizeOfRawData, psh[i].Misc.VirtualSize,
|
|---|
| 303 | psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 304 | SECTION_INITDATA);
|
|---|
| 305 | continue;
|
|---|
| 306 | }
|
|---|
| 307 | if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
|
|---|
| 308 | cout << "Other Section, stored as read/write uninit data" << endl << endl;
|
|---|
| 309 | OS2Exe.StoreSection((char *)win32file+psh[i].PointerToRawData,
|
|---|
| 310 | psh[i].SizeOfRawData, psh[i].Misc.VirtualSize,
|
|---|
| 311 | psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 312 | SECTION_UNINITDATA);
|
|---|
| 313 | continue;
|
|---|
| 314 | }
|
|---|
| 315 | }
|
|---|
| 316 | }
|
|---|
| 317 | //SvL: Align and/or concatenate code & data sections
|
|---|
| 318 | OS2Exe.AlignSections();
|
|---|
| 319 |
|
|---|
| 320 | prel = (PIMAGE_BASE_RELOCATION) ImageDirectoryOffset (win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
|
|---|
| 321 | OS2Exe.SetNrOff32Fixups((prel) ? oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size/2 : 0);
|
|---|
| 322 |
|
|---|
| 323 | if(prel) {
|
|---|
| 324 | j = 1;
|
|---|
| 325 | while(prel->VirtualAddress) {
|
|---|
| 326 | page = (char *)((char *)prel + (int)prel->VirtualAddress);
|
|---|
| 327 | count = (prel->SizeOfBlock - 8)/2;
|
|---|
| 328 | cout.setf(ios::hex, ios::basefield);
|
|---|
| 329 | cout << "Page " << j << " Address " << (ULONG)prel->VirtualAddress << " Count " << count << endl;
|
|---|
| 330 | cout.setf(ios::dec, ios::basefield);
|
|---|
| 331 | j++;
|
|---|
| 332 | for(i=0;i<count;i++) {
|
|---|
| 333 | int type = prel->TypeOffset[i] >> 12;
|
|---|
| 334 | int offset = prel->TypeOffset[i] & 0xFFF;
|
|---|
| 335 | switch(type) {
|
|---|
| 336 | case IMAGE_REL_BASED_ABSOLUTE:
|
|---|
| 337 | //// cout << "absolute fixup; unused" << endl;
|
|---|
| 338 | break; //skip
|
|---|
| 339 | case IMAGE_REL_BASED_HIGHLOW:
|
|---|
| 340 | //// cout << "address " << offset << " type " << type << endl;
|
|---|
| 341 | OS2Exe.AddOff32Fixup(oh.ImageBase +
|
|---|
| 342 | prel->VirtualAddress +
|
|---|
| 343 | offset);
|
|---|
| 344 | break;
|
|---|
| 345 | case IMAGE_REL_BASED_HIGH:
|
|---|
| 346 | case IMAGE_REL_BASED_LOW:
|
|---|
| 347 | case IMAGE_REL_BASED_HIGHADJ:
|
|---|
| 348 | case IMAGE_REL_BASED_MIPS_JMPADDR:
|
|---|
| 349 | default:
|
|---|
| 350 | cout << "Unknown/unsupported fixup type!" << endl;
|
|---|
| 351 | break;
|
|---|
| 352 | }
|
|---|
| 353 | }
|
|---|
| 354 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
|---|
| 355 | }//while
|
|---|
| 356 | }
|
|---|
| 357 | else {
|
|---|
| 358 | cout << "No fixups, might not run!" << endl;
|
|---|
| 359 | OS2Exe.SetNoFixups();
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /* get section header and pointer to data directory for .edata section */
|
|---|
| 363 | if((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset
|
|---|
| 364 | (win32file, IMAGE_DIRECTORY_ENTRY_EXPORT)) != NULL &&
|
|---|
| 365 | (fh.Characteristics & IMAGE_FILE_DLL) &&
|
|---|
| 366 | GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_EXPORT, &sh) ) {
|
|---|
| 367 |
|
|---|
| 368 | cout << "Exported Functions: " << endl;
|
|---|
| 369 | ptrOrd = (USHORT *)((int)ped->AddressOfNameOrdinals -
|
|---|
| 370 | (int)sh.VirtualAddress +
|
|---|
| 371 | (int)sh.PointerToRawData + (int)win32file);
|
|---|
| 372 | ptrNames = (int *)((int)ped->AddressOfNames -
|
|---|
| 373 | (int)sh.VirtualAddress +
|
|---|
| 374 | (int)sh.PointerToRawData + (int)win32file);
|
|---|
| 375 | ptrAddress = (int *)((int)ped->AddressOfFunctions -
|
|---|
| 376 | (int)sh.VirtualAddress +
|
|---|
| 377 | (int)sh.PointerToRawData + (int)win32file);
|
|---|
| 378 | OS2Exe.SetNrExtFixups(max(ped->NumberOfNames,ped->NumberOfFunctions));
|
|---|
| 379 | int ord, RVAExport;
|
|---|
| 380 | char *name;
|
|---|
| 381 | for(i=0;i<ped->NumberOfNames;i++) {
|
|---|
| 382 | ord = ptrOrd[i] + ped->Base;
|
|---|
| 383 | name = (char *)((int)ptrNames[i] - (int)sh.VirtualAddress +
|
|---|
| 384 | (int)sh.PointerToRawData + (int)win32file);
|
|---|
| 385 | RVAExport = ptrAddress[ptrOrd[i]];
|
|---|
| 386 | // RVAExport = ptrAddress[i];
|
|---|
| 387 | #ifdef FORWARDERS
|
|---|
| 388 | if(RVAExport < sh.VirtualAddress || RVAExport > sh.VirtualAddress + sh.SizeOfRawData) {
|
|---|
| 389 | #endif
|
|---|
| 390 | //points to code (virtual address relative to oh.ImageBase
|
|---|
| 391 | cout << "address 0x";
|
|---|
| 392 | cout.setf(ios::hex, ios::basefield);
|
|---|
| 393 | cout << RVAExport;
|
|---|
| 394 | cout.setf(ios::dec, ios::basefield);
|
|---|
| 395 | cout << " " << name << "@" << ord << endl;
|
|---|
| 396 | OS2Exe.AddNameExport(oh.ImageBase + RVAExport, name, ord);
|
|---|
| 397 | #ifdef FORWARDERS
|
|---|
| 398 |
|
|---|
| 399 | }
|
|---|
| 400 | else {//forwarder
|
|---|
| 401 | char *forward = (char *)((int)RVAExport -
|
|---|
| 402 | (int)sh.VirtualAddress +
|
|---|
| 403 | (int)sh.PointerToRawData +
|
|---|
| 404 | (int)win32file);
|
|---|
| 405 | cout << RVAExport << " " << name << " @" << ord << " is forwarder to " << (int)forward << endl;
|
|---|
| 406 | // OS2Exe.AddForwarder(name, ord, forward);
|
|---|
| 407 | }
|
|---|
| 408 | #endif
|
|---|
| 409 | }
|
|---|
| 410 | for(i=0;i<max(ped->NumberOfNames,ped->NumberOfFunctions);i++) {
|
|---|
| 411 | ord = ped->Base + i; //Correct??
|
|---|
| 412 | RVAExport = ptrAddress[i];
|
|---|
| 413 | #ifdef FORWARDERS
|
|---|
| 414 | if(RVAExport < sh.VirtualAddress || RVAExport > sh.VirtualAddress + sh.SizeOfRawData) {
|
|---|
| 415 | #endif
|
|---|
| 416 | if(RVAExport) {
|
|---|
| 417 | //points to code (virtual address relative to oh.ImageBase
|
|---|
| 418 | cout << "ord " << ord << " at 0x";
|
|---|
| 419 | cout.setf(ios::hex, ios::basefield);
|
|---|
| 420 | cout << RVAExport << endl;
|
|---|
| 421 | cout.setf(ios::dec, ios::basefield);
|
|---|
| 422 | OS2Exe.AddOrdExport(oh.ImageBase + RVAExport, ord);
|
|---|
| 423 | }
|
|---|
| 424 | #ifdef FORWARDERS
|
|---|
| 425 | }
|
|---|
| 426 | else {//forwarder or empty
|
|---|
| 427 | char *forward = (char *)((int)RVAExport -
|
|---|
| 428 | (int)sh.VirtualAddress +
|
|---|
| 429 | (int)sh.PointerToRawData +
|
|---|
| 430 | (int)win32file);
|
|---|
| 431 | cout << "ord " << ord << " at 0x";
|
|---|
| 432 | cout.setf(ios::hex, ios::basefield);
|
|---|
| 433 | cout << RVAExport << " is forwarder to 0x" << (int)forward << endl;
|
|---|
| 434 | cout.setf(ios::dec, ios::basefield);
|
|---|
| 435 | // OS2Exe.AddForwarder(NULL, ord, forward);
|
|---|
| 436 | }
|
|---|
| 437 | #endif
|
|---|
| 438 | }
|
|---|
| 439 | }
|
|---|
| 440 | else {
|
|---|
| 441 | if(fh.Characteristics & IMAGE_FILE_DLL) {
|
|---|
| 442 | OS2Exe.SetNrExtFixups(0); //adds module name to resident table
|
|---|
| 443 | }
|
|---|
| 444 | }
|
|---|
| 445 | //imported stuff
|
|---|
| 446 | if (!ProcessImports(win32file))
|
|---|
| 447 | {
|
|---|
| 448 | cout << "Fatal: failed to process imports" << endl;
|
|---|
| 449 | return -1;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 | //resource stuff
|
|---|
| 454 | if(GetSectionHdrByName (win32file, &sh, ".rsrc")) {
|
|---|
| 455 | cout << "Resource section: " << endl;
|
|---|
| 456 | cout << "Raw data size: " << hex(sh.SizeOfRawData) << endl;
|
|---|
| 457 | cout << "Virtual Address: " << hex(sh.VirtualAddress) << endl;
|
|---|
| 458 | cout << "Pointer to raw data: " << hex(sh.PointerToRawData) << endl;
|
|---|
| 459 | cout << "Section flags: " << hex(sh.Characteristics) << endl << endl;
|
|---|
| 460 | /* get root directory of resource tree */
|
|---|
| 461 | if(NULL != (prdRoot = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
|
|---|
| 462 | (win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE))) {
|
|---|
| 463 | cout << "Resources: " << endl;
|
|---|
| 464 | cout << "pdrRoot: " << hex((ULONG)prdRoot - (ULONG)win32file) << endl;
|
|---|
| 465 |
|
|---|
| 466 | OS2Exe.SetResourceSize(max(sh.Misc.VirtualSize, sh.SizeOfRawData));
|
|---|
| 467 | OS2Exe.SetNrResources(GetNumberOfResources(win32file));
|
|---|
| 468 |
|
|---|
| 469 | /* set pointer to first resource type entry */
|
|---|
| 470 | prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdRoot + sizeof (IMAGE_RESOURCE_DIRECTORY));
|
|---|
| 471 |
|
|---|
| 472 | //SvL: Store all resource ids so we can safely allocate unique ids for name ids
|
|---|
| 473 | for (i=0; i<prdRoot->NumberOfNamedEntries+prdRoot->NumberOfIdEntries; i++) {
|
|---|
| 474 | /* locate directory or each resource type */
|
|---|
| 475 | prdType = (PIMAGE_RESOURCE_DIRECTORY)((int)prdRoot + (int)prde->u2.OffsetToData);
|
|---|
| 476 |
|
|---|
| 477 | if(i<prdRoot->NumberOfNamedEntries) {
|
|---|
| 478 | //SvL: 30-10-'97, high bit is set, so clear to get real offset
|
|---|
| 479 | prde->u1.Name &= ~0x80000000;
|
|---|
| 480 | char *resname = UnicodeToAscii(*(WCHAR *)((int)prdRoot + (int)prde->u1.Name), (WCHAR *)((int)prdRoot + (int)prde->u1.Name + sizeof(WCHAR))); // first word = string length
|
|---|
| 481 |
|
|---|
| 482 | for(j=0;j<MAX_RES;j++) {
|
|---|
| 483 | if(strcmp(resname, ResTypes[j]) == 0)
|
|---|
| 484 | break;
|
|---|
| 485 | }
|
|---|
| 486 | if(j == MAX_RES) {
|
|---|
| 487 | id = NTRT_RCDATA;
|
|---|
| 488 | }
|
|---|
| 489 | else id = j;
|
|---|
| 490 | }
|
|---|
| 491 | else {
|
|---|
| 492 | id = prde->u1.Id;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | if((int)prdType & 0x80000000) {//subdirectory?
|
|---|
| 496 | StoreIdResSubDir(prdType, 1, prdRoot, sh.VirtualAddress, id, 0);
|
|---|
| 497 | }
|
|---|
| 498 | else assert(FALSE);
|
|---|
| 499 |
|
|---|
| 500 | /* increment to next entry */
|
|---|
| 501 | prde++;
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /* set pointer to first resource type entry */
|
|---|
| 505 | prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdRoot + sizeof (IMAGE_RESOURCE_DIRECTORY));
|
|---|
| 506 |
|
|---|
| 507 | /* loop through all resource directory entry types */
|
|---|
| 508 | //eerste level -> types
|
|---|
| 509 | //tweede level -> names
|
|---|
| 510 | //derde level -> language
|
|---|
| 511 | cout << "Name entries: " << endl;
|
|---|
| 512 | cout << "ID entries: " << endl;
|
|---|
| 513 | for (i=0; i<prdRoot->NumberOfNamedEntries+prdRoot->NumberOfIdEntries; i++) {
|
|---|
| 514 | /* locate directory or each resource type */
|
|---|
| 515 | prdType = (PIMAGE_RESOURCE_DIRECTORY)((int)prdRoot + (int)prde->u2.OffsetToData);
|
|---|
| 516 |
|
|---|
| 517 | if(i<prdRoot->NumberOfNamedEntries) {
|
|---|
| 518 | //SvL: 30-10-'97, high bit is set, so clear to get real offset
|
|---|
| 519 | prde->u1.Name &= ~0x80000000;
|
|---|
| 520 | char *resname = UnicodeToAscii(*(WCHAR *)((int)prdRoot + (int)prde->u1.Name), (WCHAR *)((int)prdRoot + (int)prde->u1.Name + sizeof(WCHAR))); // first word = string length
|
|---|
| 521 |
|
|---|
| 522 | cout << "Type " << resname << endl;
|
|---|
| 523 | for(j=0;j<MAX_RES;j++) {
|
|---|
| 524 | if(strcmp(resname, ResTypes[j]) == 0)
|
|---|
| 525 | break;
|
|---|
| 526 | }
|
|---|
| 527 | if(j == MAX_RES) {
|
|---|
| 528 | id = NTRT_RCDATA;
|
|---|
| 529 | }
|
|---|
| 530 | else id = j;
|
|---|
| 531 | }
|
|---|
| 532 | else {
|
|---|
| 533 | if(prde->u1.Id < MAX_RES)
|
|---|
| 534 | cout << "Type " << ResTypes[prde->u1.Id] << endl;
|
|---|
| 535 | else cout << "Custom Type " << prde->u1.Id << endl;
|
|---|
| 536 | id = prde->u1.Id;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | if((int)prdType & 0x80000000) {//subdirectory?
|
|---|
| 540 | ProcessResSubDir(prdType, 1, prdRoot, sh.VirtualAddress, id, 0);
|
|---|
| 541 | }
|
|---|
| 542 | else {
|
|---|
| 543 | pData = (PIMAGE_RESOURCE_DATA_ENTRY)prdType;
|
|---|
| 544 | cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
|
|---|
| 545 | cout << "Resource Data entry of size " << hex(pData->Size) << endl;
|
|---|
| 546 | cout << "Resource Data RVA " << hex(pData->OffsetToData) << endl;
|
|---|
| 547 | cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
|
|---|
| 548 | assert(FALSE);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | /* increment to next entry */
|
|---|
| 552 | prde++;
|
|---|
| 553 | }
|
|---|
| 554 | }
|
|---|
| 555 | OS2Icon::DestroyAll();
|
|---|
| 556 | OS2Cursor::DestroyAll();
|
|---|
| 557 | }
|
|---|
| 558 | OS2Exe.SaveConvertedNames();
|
|---|
| 559 | if(argc == 2)
|
|---|
| 560 | OS2Exe.SaveNewExeFile(argv[1]);
|
|---|
| 561 | else OS2Exe.SaveNewExeFile(argv[2]);
|
|---|
| 562 |
|
|---|
| 563 | return(0);
|
|---|
| 564 | }
|
|---|
| 565 | //******************************************************************************
|
|---|
| 566 | //No support for multiple languages; just select the first one present and
|
|---|
| 567 | //ignore the rest
|
|---|
| 568 | //******************************************************************************
|
|---|
| 569 | void ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType, int level,
|
|---|
| 570 | PIMAGE_RESOURCE_DIRECTORY prdRoot, int VirtualAddress,
|
|---|
| 571 | int type, int id)
|
|---|
| 572 | {
|
|---|
| 573 | PIMAGE_RESOURCE_DIRECTORY prdType2;
|
|---|
| 574 | PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
|
|---|
| 575 | PIMAGE_RESOURCE_DIR_STRING_U pstring;
|
|---|
| 576 | PIMAGE_RESOURCE_DATA_ENTRY pData;
|
|---|
| 577 | int i;
|
|---|
| 578 |
|
|---|
| 579 | switch(level) {
|
|---|
| 580 | case 1:
|
|---|
| 581 | cout << "Names.." << endl;
|
|---|
| 582 | break;
|
|---|
| 583 | case 2:
|
|---|
| 584 | cout << "Language.." << endl;
|
|---|
| 585 | break;
|
|---|
| 586 | }
|
|---|
| 587 | prdType = (PIMAGE_RESOURCE_DIRECTORY)((int)prdType & ~0x80000000);
|
|---|
| 588 | cout << "Subdir with " << prdType->NumberOfNamedEntries << " Name entries" << endl;
|
|---|
| 589 | cout << "Subdir with " << prdType->NumberOfIdEntries << " id entries" << endl;
|
|---|
| 590 |
|
|---|
| 591 | prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY));
|
|---|
| 592 | for(i=0;i<prdType->NumberOfNamedEntries+prdType->NumberOfIdEntries;i++) {
|
|---|
| 593 | /* locate directory or each resource type */
|
|---|
| 594 | prdType2 = (PIMAGE_RESOURCE_DIRECTORY)((int)prdRoot + (int)prde->u2.OffsetToData);
|
|---|
| 595 |
|
|---|
| 596 | if(i < prdType->NumberOfNamedEntries) {//name or id entry?
|
|---|
| 597 | if(prde->u1.s.NameIsString) //unicode directory string /*PLF Sat 97-06-21 22:30:35*/
|
|---|
| 598 | prde->u1.Name &= ~0x80000000;
|
|---|
| 599 | pstring = (PIMAGE_RESOURCE_DIR_STRING_U)((int)prdRoot + (int)prde->u1.Name);
|
|---|
| 600 | cout << "Name = " << UnicodeToAscii(pstring->Length, pstring->NameString) << endl;
|
|---|
| 601 | if(level == 1) {
|
|---|
| 602 | id = OS2Exe.ConvertNametoId(UnicodeToAscii(pstring->Length, pstring->NameString));
|
|---|
| 603 | }
|
|---|
| 604 | }
|
|---|
| 605 | else {
|
|---|
| 606 | cout << "Id " << prde->u1.Id << endl;
|
|---|
| 607 | if(level == 1) id = prde->u1.Id;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | if((int)prdType2 & 0x80000000) {//subdirectory?
|
|---|
| 611 | ProcessResSubDir(prdType2, 2, prdRoot, VirtualAddress, type, id);
|
|---|
| 612 | }
|
|---|
| 613 | else {
|
|---|
| 614 | pData = (PIMAGE_RESOURCE_DATA_ENTRY)prdType2;
|
|---|
| 615 | cout << "Resource Data entry of size " << hex(pData->Size) << endl;
|
|---|
| 616 | cout << "Resource Data RVA " << hex(pData->OffsetToData - VirtualAddress) << endl;
|
|---|
| 617 | cout << "Resource Data RVA " << hex(pData->OffsetToData) << endl;
|
|---|
| 618 | cout << "Resource Data VA " << hex(VirtualAddress) << endl;
|
|---|
| 619 | cout << "Resource Codepage " << pData->CodePage << endl;
|
|---|
| 620 | if(pData->Size) {//winamp17 winzip archive has resource with size 0
|
|---|
| 621 | switch(type) {
|
|---|
| 622 | case NTRT_MENU:
|
|---|
| 623 | ShowMenu(id, (MenuHeader *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 624 | break;
|
|---|
| 625 | case NTRT_ICON:
|
|---|
| 626 | new OS2Icon(id, (WINBITMAPINFOHEADER *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 627 | break;
|
|---|
| 628 | case NTRT_BITMAP:
|
|---|
| 629 | //KSO [Mon 03.08.1998]: added OS2Exe to the parameterlist
|
|---|
| 630 | ShowBitmap(OS2Exe, id, (WINBITMAPINFOHEADER *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 631 | break;
|
|---|
| 632 | case NTRT_GROUP_ICON:
|
|---|
| 633 | ShowGroupIcon(id, (IconHeader *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 634 | break;
|
|---|
| 635 | case NTRT_CURSOR:
|
|---|
| 636 | new OS2Cursor(id, (CursorComponent *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 637 | break;
|
|---|
| 638 | case NTRT_GROUP_CURSOR:
|
|---|
| 639 | ShowGroupCursor(id, (CursorHeader *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 640 | break;
|
|---|
| 641 | case NTRT_DIALOG:
|
|---|
| 642 | ShowDialog(id, (DialogBoxHeader *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 643 | break;
|
|---|
| 644 | case NTRT_VERSION:
|
|---|
| 645 | //Store version resource as OS/2 RT_RCDATA resource
|
|---|
| 646 | //to retrieve the original for win32's version apis
|
|---|
| 647 | //TODO: Only supports one version resource (who would want to use more??)
|
|---|
| 648 | OS2Exe.SetVersionResourceId(id);
|
|---|
| 649 | case NTRT_RCDATA:
|
|---|
| 650 | ShowRCData(id, (char *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 651 | break;
|
|---|
| 652 | case NTRT_STRING:
|
|---|
| 653 | //String format: tables of 16 strings stored as one resource
|
|---|
| 654 | //upper 12 bits of resource id passed by user determines block (res id)
|
|---|
| 655 | //lower 4 bits are an index into the string table
|
|---|
| 656 | //Best solution is to split the strings up and store them as RCDATA
|
|---|
| 657 | ShowStrings(id, (char *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 658 | break;
|
|---|
| 659 | case NTRT_ACCELERATORS:
|
|---|
| 660 | ShowAccelerator(id, (WINACCEL *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 661 | break;
|
|---|
| 662 | default:
|
|---|
| 663 | cout << "!!Unsupported resource type!! : " << type << endl;
|
|---|
| 664 | //Just save it as rcdata
|
|---|
| 665 | ShowRCData(id, (char *)((char *)prdRoot + pData->OffsetToData - VirtualAddress), pData->Size);
|
|---|
| 666 | break;
|
|---|
| 667 | }//switch
|
|---|
| 668 | }//if size > 0
|
|---|
| 669 | }
|
|---|
| 670 | if(level == 2) break; //only support one language!
|
|---|
| 671 |
|
|---|
| 672 | prde++;
|
|---|
| 673 | cout << endl;
|
|---|
| 674 | }
|
|---|
| 675 | }
|
|---|
| 676 | //******************************************************************************
|
|---|
| 677 | //******************************************************************************
|
|---|
| 678 | void StoreIdResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType, int level,
|
|---|
| 679 | PIMAGE_RESOURCE_DIRECTORY prdRoot, int VirtualAddress,
|
|---|
| 680 | int type, int id)
|
|---|
| 681 | {
|
|---|
| 682 | PIMAGE_RESOURCE_DIRECTORY prdType2;
|
|---|
| 683 | PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
|
|---|
| 684 | int i, j;
|
|---|
| 685 |
|
|---|
| 686 | prdType = (PIMAGE_RESOURCE_DIRECTORY)((int)prdType & ~0x80000000);
|
|---|
| 687 |
|
|---|
| 688 | prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY));
|
|---|
| 689 | for(i=0;i<prdType->NumberOfNamedEntries+prdType->NumberOfIdEntries;i++) {
|
|---|
| 690 | /* locate directory or each resource type */
|
|---|
| 691 | prdType2 = (PIMAGE_RESOURCE_DIRECTORY)((int)prdRoot + (int)prde->u2.OffsetToData);
|
|---|
| 692 |
|
|---|
| 693 | if(i >= prdType->NumberOfNamedEntries) {//name or id entry?
|
|---|
| 694 | if(level == 1) id = prde->u1.Id;
|
|---|
| 695 | }
|
|---|
| 696 | else {
|
|---|
| 697 | prde++;
|
|---|
| 698 | continue; //skip name ids
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | if((int)prdType2 & 0x80000000) {//subdirectory?
|
|---|
| 702 | StoreIdResSubDir(prdType2, 2, prdRoot, VirtualAddress, type, id);
|
|---|
| 703 | }
|
|---|
| 704 | else {
|
|---|
| 705 | if(type == NTRT_STRING) {
|
|---|
| 706 | for(j=0;j<16;j++) {
|
|---|
| 707 | OS2Exe.StoreResourceId((id-1)*16+j);
|
|---|
| 708 | }
|
|---|
| 709 | }
|
|---|
| 710 | else OS2Exe.StoreResourceId(id);
|
|---|
| 711 | }
|
|---|
| 712 | if(level == 2) break; //only support one language!
|
|---|
| 713 |
|
|---|
| 714 | prde++;
|
|---|
| 715 | }
|
|---|
| 716 | }
|
|---|
| 717 | //******************************************************************************
|
|---|
| 718 | //******************************************************************************
|
|---|
| 719 |
|
|---|
| 720 | /** All initial processing of imports is done here
|
|---|
| 721 | * Should now detect most Borland styled files including the GifCon32.exe and
|
|---|
| 722 | * loader32 from SoftIce. (Stupid Borland!!!)
|
|---|
| 723 | *
|
|---|
| 724 | * knut [Jul 22 1998 2:44am]
|
|---|
| 725 | **/
|
|---|
| 726 | BOOL ProcessImports(void *pFile)
|
|---|
| 727 | {
|
|---|
| 728 | PIMAGE_IMPORT_DESCRIPTOR pID;
|
|---|
| 729 | IMAGE_SECTION_HEADER shID;
|
|---|
| 730 | IMAGE_SECTION_HEADER shExtra = {0};
|
|---|
| 731 | PIMAGE_OPTIONAL_HEADER pOH;
|
|---|
| 732 |
|
|---|
| 733 | int i,j;
|
|---|
| 734 | BOOL fBorland = 0;
|
|---|
| 735 | int cModules;
|
|---|
| 736 | char *pszModules;
|
|---|
| 737 | char *pszCurModule;
|
|---|
| 738 | char *pszTmp;
|
|---|
| 739 | ULONG *pulImport;
|
|---|
| 740 | ULONG ulCurFixup;
|
|---|
| 741 | int Size;
|
|---|
| 742 |
|
|---|
| 743 | /* "algorithm:"
|
|---|
| 744 | * 1) get module names and store them
|
|---|
| 745 | * a) check dwRVAModuleName is within .idata seg - if not find section
|
|---|
| 746 | * 2) iterate thru functions of each module
|
|---|
| 747 | * a) check OriginalFirstThunk is not 0 and that it points to a RVA.
|
|---|
| 748 | * b) if not a) borland-styled PE-file - ARG!!!
|
|---|
| 749 | * check FirstThunk
|
|---|
| 750 | * c) check OriginalFirstThunk/FirstThunk ok RVAs and find right section
|
|---|
| 751 | * d) store ordinal/name import
|
|---|
| 752 | * 3) finished
|
|---|
| 753 | */
|
|---|
| 754 |
|
|---|
| 755 | /* 1) get module names */
|
|---|
| 756 | pID = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryOffset(pFile, IMAGE_DIRECTORY_ENTRY_IMPORT);
|
|---|
| 757 | if (pID == NULL) {
|
|---|
| 758 | OS2Exe.StoreImportModules("KERNEL32.DLL", 1);
|
|---|
| 759 | OS2Exe.SetNoNameImports();
|
|---|
| 760 | return TRUE; //SvL: yes, there are dlls without imports!
|
|---|
| 761 | }
|
|---|
| 762 | if (!GetSectionHdrByImageDir(pFile, IMAGE_DIRECTORY_ENTRY_IMPORT, &shID)) {
|
|---|
| 763 | OS2Exe.StoreImportModules("KERNEL32.DLL", 1);
|
|---|
| 764 | OS2Exe.SetNoNameImports();
|
|---|
| 765 | return TRUE; //SvL: yes, there are dlls without imports!
|
|---|
| 766 | }
|
|---|
| 767 | //calc size of module list
|
|---|
| 768 | i = Size = cModules = 0;
|
|---|
| 769 | while (pID[i].Name != 0)
|
|---|
| 770 | {
|
|---|
| 771 | //test RVA inside ID-Section
|
|---|
| 772 | if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData))
|
|---|
| 773 | pszTmp = (char*)(pID[i].Name- shID.VirtualAddress + shID.PointerToRawData + (ULONG)pFile);
|
|---|
| 774 | else
|
|---|
| 775 | {
|
|---|
| 776 | //is the "Extra"-section already found or do we have to find it?
|
|---|
| 777 | if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 778 | if (!GetSectionHdrByRVA(pFile, &shExtra, pID[i].Name))
|
|---|
| 779 | return FALSE;
|
|---|
| 780 | pszTmp = (char*)(pID[i].Name- shExtra.VirtualAddress + shExtra.PointerToRawData + (ULONG)pFile);
|
|---|
| 781 | }
|
|---|
| 782 | Size += strlen(pszTmp) + 1;
|
|---|
| 783 | i++;
|
|---|
| 784 | cModules++;
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | pszModules = (char*)malloc(Size);
|
|---|
| 788 | assert(pszModules != NULL);
|
|---|
| 789 | j = 0;
|
|---|
| 790 | for (i = 0; i < cModules; i++)
|
|---|
| 791 | {
|
|---|
| 792 | //test RVA inside ID-Section
|
|---|
| 793 | if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData))
|
|---|
| 794 | pszTmp = (char*)(pID[i].Name- shID.VirtualAddress + shID.PointerToRawData + (ULONG)pFile);
|
|---|
| 795 | else
|
|---|
| 796 | {
|
|---|
| 797 | fBorland = TRUE;
|
|---|
| 798 | //is the "Extra"-section already found or do we have to find it?
|
|---|
| 799 | if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 800 | if (GetSectionHdrByRVA(pFile, &shExtra, pID[i].Name))
|
|---|
| 801 | {
|
|---|
| 802 | free(pszModules);
|
|---|
| 803 | return FALSE;
|
|---|
| 804 | }
|
|---|
| 805 | pszTmp = (char*)(pID[i].Name- shExtra.VirtualAddress + shExtra.PointerToRawData + (ULONG)pFile);
|
|---|
| 806 | }
|
|---|
| 807 | strcpy(pszModules+j, pszTmp);
|
|---|
| 808 | j += strlen(pszTmp) + 1;
|
|---|
| 809 | }
|
|---|
| 810 | cout << endl;
|
|---|
| 811 | if (fBorland)
|
|---|
| 812 | cout << "Borland-styled PE-File." << endl;
|
|---|
| 813 | //Store modules
|
|---|
| 814 | cout << cModules << " imported Modules: " << endl;
|
|---|
| 815 | OS2Exe.StoreImportModules(pszModules, cModules);
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 | /* 2) functions */
|
|---|
| 819 | pszCurModule = pszModules;
|
|---|
| 820 | pOH = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF(pFile);
|
|---|
| 821 | for (i = 0; i < cModules; i++)
|
|---|
| 822 | {
|
|---|
| 823 | cout << "Module " << pszCurModule << endl;
|
|---|
| 824 | // a) check that OriginalFirstThunk not is 0 and look for Borland-styled PE
|
|---|
| 825 | if (i == 0)
|
|---|
| 826 | {
|
|---|
| 827 | //heavy borland-style test - assume array of thunks is within that style does not change
|
|---|
| 828 | if ((ULONG)pID[i].u.OriginalFirstThunk == 0
|
|---|
| 829 | ////|| (ULONG)pID[i].u.OriginalFirstThunk < pOH->SizeOfImage
|
|---|
| 830 | || (ULONG)pID[i].u.OriginalFirstThunk < shID.VirtualAddress
|
|---|
| 831 | || (ULONG)pID[i].u.OriginalFirstThunk >= shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)
|
|---|
| 832 | || (ULONG)pID[i].u.OriginalFirstThunk >= pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
|
|---|
| 833 | && (ULONG)pID[i].u.OriginalFirstThunk < sizeof(*pID)*cModules + pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
|
|---|
| 834 | // && (ULONG)pID[i].u.OriginalFirstThunk < pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size + pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
|
|---|
| 835 | )
|
|---|
| 836 | fBorland = TRUE;
|
|---|
| 837 | }
|
|---|
| 838 |
|
|---|
| 839 | //light borland-style test
|
|---|
| 840 | if (pID[i].u.OriginalFirstThunk == 0 || fBorland)
|
|---|
| 841 | pulImport = (ULONG*)pID[i].FirstThunk;
|
|---|
| 842 | else
|
|---|
| 843 | pulImport = (ULONG*)pID[i].u.OriginalFirstThunk;
|
|---|
| 844 |
|
|---|
| 845 | // b) check if RVA ok
|
|---|
| 846 | if (!(pulImport > 0 && (ULONG)pulImport < pOH->SizeOfImage))
|
|---|
| 847 | {
|
|---|
| 848 | cout << "Invalid RVA " << hex((ULONG)pulImport) << endl;
|
|---|
| 849 | break;
|
|---|
| 850 | }
|
|---|
| 851 | // check section
|
|---|
| 852 | if ((ULONG)pulImport < shExtra.VirtualAddress || (ULONG)pulImport >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 853 | {
|
|---|
| 854 | if (!GetSectionHdrByRVA(pFile, &shExtra, (ULONG)pulImport))
|
|---|
| 855 | {
|
|---|
| 856 | cout << "warning: could not find section for Thunk RVA " << hex((ULONG)pulImport) << endl;
|
|---|
| 857 | break;
|
|---|
| 858 | }
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | pulImport = (PULONG)((ULONG)pulImport - shExtra.VirtualAddress + (ULONG)pFile + shExtra.PointerToRawData);
|
|---|
| 862 | j = 0;
|
|---|
| 863 | ulCurFixup = (ULONG)pID[i].FirstThunk + pOH->ImageBase;
|
|---|
| 864 | while (pulImport[j] != 0)
|
|---|
| 865 | {
|
|---|
| 866 | if (pulImport[j] & IMAGE_ORDINAL_FLAG)
|
|---|
| 867 | { //ordinal
|
|---|
| 868 | cout.setf(ios::hex, ios::basefield);
|
|---|
| 869 | cout << "0x" << ulCurFixup << " Imported function " << pszCurModule << "@" << (pulImport[j] & ~IMAGE_ORDINAL_FLAG) << endl;
|
|---|
| 870 | cout.setf(ios::dec, ios::basefield);
|
|---|
| 871 | OS2Exe.StoreImportByOrd(i, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup);
|
|---|
| 872 | }
|
|---|
| 873 | else
|
|---|
| 874 | { //name
|
|---|
| 875 | //check
|
|---|
| 876 | if (pulImport[j] < shExtra.VirtualAddress || pulImport[j] >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 877 | if (!GetSectionHdrByRVA(pFile, &shExtra, pulImport[j]))
|
|---|
| 878 | {
|
|---|
| 879 | cout << "warning: could not find section for Import Name RVA " << hex(pulImport[j]) << endl;
|
|---|
| 880 | break;
|
|---|
| 881 | }
|
|---|
| 882 | //KSO - Aug 6 1998 1:15am:this eases comparing...
|
|---|
| 883 | char *pszFunctionName = (char*)(pulImport[j] + (ULONG)pFile + shExtra.PointerToRawData - shExtra.VirtualAddress + 2);
|
|---|
| 884 | cout.setf(ios::hex, ios::basefield);
|
|---|
| 885 | cout << "0x" << ulCurFixup << " Imported function " << pszFunctionName << endl;
|
|---|
| 886 | cout.setf(ios::dec, ios::basefield);
|
|---|
| 887 | OS2Exe.StoreImportByName(pszCurModule, i, pszFunctionName, ulCurFixup);
|
|---|
| 888 | }
|
|---|
| 889 | ulCurFixup += sizeof(IMAGE_THUNK_DATA);
|
|---|
| 890 | j++;
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | pszCurModule += strlen(pszCurModule) + 1;
|
|---|
| 894 | cout << endl;
|
|---|
| 895 | }//for (i = 0; i < cModules; i++)
|
|---|
| 896 |
|
|---|
| 897 | free(pszModules);
|
|---|
| 898 | return TRUE;
|
|---|
| 899 | }
|
|---|
| 900 |
|
|---|