| 1 | /* $Id: pe2lxmain.cpp,v 1.9 2003-03-31 02:52:50 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * Pe2Lx main program. (Ring 3 only!)
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1999 knut st. osmundsen
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | /*******************************************************************************
|
|---|
| 12 | * Defined Constants And Macros *
|
|---|
| 13 | *******************************************************************************/
|
|---|
| 14 | #define FOR_EXEHDR 1
|
|---|
| 15 | #define INCL_BASE
|
|---|
| 16 | #define INCL_DOSFILEMGR
|
|---|
| 17 | #define INCL_DOSERRORS
|
|---|
| 18 | #define INCL_OS2KRNL_LDR
|
|---|
| 19 |
|
|---|
| 20 | #define DATA16_GLOBAL
|
|---|
| 21 | #define OUTPUT_COM2 0x2f8
|
|---|
| 22 |
|
|---|
| 23 | /*******************************************************************************
|
|---|
| 24 | * Header Files *
|
|---|
| 25 | *******************************************************************************/
|
|---|
| 26 | #include <os2.h>
|
|---|
| 27 | #include <peexe.h>
|
|---|
| 28 | #include <neexe.h>
|
|---|
| 29 | #include <newexe.h>
|
|---|
| 30 | #include <exe386.h>
|
|---|
| 31 | #include <malloc.h>
|
|---|
| 32 | #include <string.h>
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #include <io.h>
|
|---|
| 35 | #include <fcntl.h>
|
|---|
| 36 | #include <sys/stat.h>
|
|---|
| 37 | #include "OS2Krnl.h"
|
|---|
| 38 | #include "modulebase.h"
|
|---|
| 39 | #include "pe2lx.h"
|
|---|
| 40 | #include "options.h"
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <versionos2.h>
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /*******************************************************************************
|
|---|
| 46 | * Global Variables *
|
|---|
| 47 | *******************************************************************************/
|
|---|
| 48 | char szBackupWin32Filename[CCHMAXPATH]; /* too save stack/heap */
|
|---|
| 49 | struct options options = DEFAULT_OPTION_ASSIGMENTS;
|
|---|
| 50 |
|
|---|
| 51 | /*******************************************************************************
|
|---|
| 52 | * Internal Functions *
|
|---|
| 53 | *******************************************************************************/
|
|---|
| 54 | static void syntax();
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Main function of the Pe2Lx utillity.
|
|---|
| 59 | * @returns 0 on success.
|
|---|
| 60 | * 1 Help.
|
|---|
| 61 | * 2 Syntax error: Invalid argument, '<arg>'.
|
|---|
| 62 | * 3 Syntax error: Too many filenames.
|
|---|
| 63 | * 4 Syntax error: No Win32-file specified.
|
|---|
| 64 | * 80 Fatal error: Can't find Win32-file, <filename>.
|
|---|
| 65 | * 81 Fatal error: Failed to rename the Win32-file, <from> -> <to>
|
|---|
| 66 | * 82 Fatal error: Failed to open Win32-file, <filename>. rc=<rc>
|
|---|
| 67 | * 83 Fatal error: Failed to convertert the file. rc=<rc from init(..)>
|
|---|
| 68 | * 84 Fatal error: Failed to write the Lx-file. rc=<rc from writeFile(..)>
|
|---|
| 69 | * @param argc Count of arguments.
|
|---|
| 70 | * @param argv Array of argument pointers. argc entries.
|
|---|
| 71 | * @status completely implemented.
|
|---|
| 72 | * @author knut st. osmundsen
|
|---|
| 73 | */
|
|---|
| 74 | int main(int argc, char **argv)
|
|---|
| 75 | {
|
|---|
| 76 | APIRET rc;
|
|---|
| 77 | ULONG ulAction = 0;
|
|---|
| 78 | PCSZ pszWin32Filename = NULL;
|
|---|
| 79 | PCSZ pszOdin32Filename = NULL;
|
|---|
| 80 | PCSZ psz;
|
|---|
| 81 | int argi;
|
|---|
| 82 |
|
|---|
| 83 | /* special edition for Dave Evans */
|
|---|
| 84 | options.fPEOneObject = FLAGS_PEOO_DISABLED;
|
|---|
| 85 |
|
|---|
| 86 | /* read parameters */
|
|---|
| 87 | for (argi = 1; argi < argc; argi++)
|
|---|
| 88 | {
|
|---|
| 89 | /* check if option or filname */
|
|---|
| 90 | if (argv[argi][0] == '-' || argv[argi][0] == '/')
|
|---|
| 91 | { /* option */
|
|---|
| 92 | switch (argv[argi][1])
|
|---|
| 93 | {
|
|---|
| 94 | case '1': /* All-In-One-Object fix - temporary...- -1<-|+|*> */
|
|---|
| 95 | if (argv[argi][2] == '-')
|
|---|
| 96 | options.fPEOneObject = FLAGS_PEOO_DISABLED;
|
|---|
| 97 | else if (argv[argi][2] == '+')
|
|---|
| 98 | options.fPEOneObject = FLAGS_PEOO_ENABLED;
|
|---|
| 99 | else
|
|---|
| 100 | options.fPEOneObject = FLAGS_PEOO_FORCED;
|
|---|
| 101 | break;
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | case 'h': /* syntax help */
|
|---|
| 105 | case 'H':
|
|---|
| 106 | case '?':
|
|---|
| 107 | syntax();
|
|---|
| 108 | return 1;
|
|---|
| 109 |
|
|---|
| 110 | case 'w': /* warning level */
|
|---|
| 111 | case 'W':
|
|---|
| 112 | psz = argv[argi] + (argv[argi][2] == ':' || argv[argi][2] == '=') + 2;
|
|---|
| 113 | if (*psz >= '0' && *psz <= '4' && psz[1] == '\0')
|
|---|
| 114 | {
|
|---|
| 115 | switch (*psz)
|
|---|
| 116 | {
|
|---|
| 117 | case '0': ModuleBase::ulInfoLevel = ModuleBase::Quiet; break;
|
|---|
| 118 | case '1': ModuleBase::ulInfoLevel = ModuleBase::Error; break;
|
|---|
| 119 | case '2': ModuleBase::ulInfoLevel = ModuleBase::Warning; break;
|
|---|
| 120 | case '3': ModuleBase::ulInfoLevel = ModuleBase::Info; break;
|
|---|
| 121 | case '4': ModuleBase::ulInfoLevel = ModuleBase::InfoAll; break;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | else
|
|---|
| 125 | {
|
|---|
| 126 | printf("Syntax error: Incorrect use of argument '%.2s'.\n\n", argv[argi]);
|
|---|
| 127 | return 5;
|
|---|
| 128 | }
|
|---|
| 129 | break;
|
|---|
| 130 |
|
|---|
| 131 | case 'f': /* ignore internal fixups */
|
|---|
| 132 | case 'F':
|
|---|
| 133 | options.fSkipFixups = TRUE;
|
|---|
| 134 | break;
|
|---|
| 135 |
|
|---|
| 136 | case 'r': /* all read-write objects */
|
|---|
| 137 | case 'R':
|
|---|
| 138 | if (argv[argi][2] != 'w' && argv[argi][2] != 'W')
|
|---|
| 139 | {
|
|---|
| 140 | printf("Syntax error: Invalid argument, '%s'\n", argv[argi]);
|
|---|
| 141 | return 5;
|
|---|
| 142 | }
|
|---|
| 143 | options.fAllRWObjects = TRUE;
|
|---|
| 144 | break;
|
|---|
| 145 |
|
|---|
| 146 | case 'c': /* custom odin dll name */
|
|---|
| 147 | case 'C':
|
|---|
| 148 | if (!hasCustomExports())
|
|---|
| 149 | {
|
|---|
| 150 | printf("Syntax error: export table file not specified (-o:).\n\n");
|
|---|
| 151 | return 5;
|
|---|
| 152 | }
|
|---|
| 153 | options.pszCustomDll = &argv[argi][3];
|
|---|
| 154 | break;
|
|---|
| 155 |
|
|---|
| 156 | case 'o': /* custom odin dll ordinal mapping */
|
|---|
| 157 | case 'O':
|
|---|
| 158 | {
|
|---|
| 159 | int fileIn = open(&argv[argi][3], O_RDONLY, S_IREAD);
|
|---|
| 160 | int sizein = (int)_filelength(fileIn);
|
|---|
| 161 |
|
|---|
| 162 | options.pszCustomExports = (PSZ)malloc(sizein+1);
|
|---|
| 163 | memset(options.pszCustomExports, 0, sizein+1);
|
|---|
| 164 | read(fileIn, options.pszCustomExports, sizein);
|
|---|
| 165 | close(fileIn);
|
|---|
| 166 | break;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | case 'x': /* custombuild exclude dll */
|
|---|
| 170 | case 'X':
|
|---|
| 171 | {
|
|---|
| 172 | int cch = strlen(&argv[argi][3]);
|
|---|
| 173 | if (!cch)
|
|---|
| 174 | {
|
|---|
| 175 | printf("Syntax error: optino -x: requires a dll name!");
|
|---|
| 176 | return 5;
|
|---|
| 177 | }
|
|---|
| 178 | int cchNew = cch + 4;
|
|---|
| 179 | if (options.pszCustomDllExclude)
|
|---|
| 180 | cchNew += strlen(options.pszCustomDllExclude);
|
|---|
| 181 | options.pszCustomDllExclude = (char*)realloc(options.pszCustomDllExclude, cchNew);
|
|---|
| 182 |
|
|---|
| 183 | char *psz = options.pszCustomDllExclude;
|
|---|
| 184 | if (cchNew != cch + 4)
|
|---|
| 185 | psz = psz + strlen(psz);
|
|---|
| 186 |
|
|---|
| 187 | /* copy the name in uppercase with ';' at both ends. */
|
|---|
| 188 | *psz++ = ';';
|
|---|
| 189 | for (strcpy(psz, &argv[argi][3]); *psz; psz++)
|
|---|
| 190 | if (*psz >= 'a' && *psz <= 'z')
|
|---|
| 191 | *psz += ('A' - 'a');
|
|---|
| 192 | *psz++ =';';
|
|---|
| 193 | *psz = '\0';
|
|---|
| 194 | break;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | default:
|
|---|
| 198 | printf("Syntax error: Invalid argument, '%s'\n", argv[argi]);
|
|---|
| 199 | syntax();
|
|---|
| 200 | return 2;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | else
|
|---|
| 204 | {
|
|---|
| 205 | if (pszWin32Filename == NULL)
|
|---|
| 206 | {
|
|---|
| 207 | pszWin32Filename = argv[argi];
|
|---|
| 208 | /* check if exists */
|
|---|
| 209 | rc = DosQueryPathInfo(pszWin32Filename,FIL_QUERYFULLNAME,
|
|---|
| 210 | &szBackupWin32Filename[0], sizeof(szBackupWin32Filename));
|
|---|
| 211 | if (rc != NO_ERROR)
|
|---|
| 212 | {
|
|---|
| 213 | printf("Fatal error: Can't find Win32-file, '%s'.\n", pszWin32Filename);
|
|---|
| 214 | return 80;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | else if (pszOdin32Filename == NULL)
|
|---|
| 218 | pszOdin32Filename = argv[argi];
|
|---|
| 219 | else
|
|---|
| 220 | {
|
|---|
| 221 | printf("Syntax error: Too many filenames!\n");
|
|---|
| 222 | syntax();
|
|---|
| 223 | return 3;
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 | } /* for */
|
|---|
| 227 |
|
|---|
| 228 | /* check if enough arguments */
|
|---|
| 229 | if (pszWin32Filename == NULL)
|
|---|
| 230 | {
|
|---|
| 231 | printf("Syntax error: No Win32-file specified.\n\n");
|
|---|
| 232 | syntax();
|
|---|
| 233 | return 4;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /* rename files? */
|
|---|
| 237 | if (pszOdin32Filename == NULL)
|
|---|
| 238 | {
|
|---|
| 239 | char *pszExt = strrchr(pszWin32Filename, '.');
|
|---|
| 240 | if (pszExt == NULL)
|
|---|
| 241 | {
|
|---|
| 242 | printf("warning: Win32-file don't have an extention\n");
|
|---|
| 243 | strcpy(szBackupWin32Filename, pszWin32Filename);
|
|---|
| 244 | strcat(szBackupWin32Filename, "wbk");
|
|---|
| 245 | }
|
|---|
| 246 | else
|
|---|
| 247 | {
|
|---|
| 248 | memset(szBackupWin32Filename, 0, sizeof(szBackupWin32Filename));
|
|---|
| 249 | if (stricmp(pszExt + 1, "exe") == 0)
|
|---|
| 250 | {
|
|---|
| 251 | strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 1);
|
|---|
| 252 | strcat(szBackupWin32Filename, "exf");
|
|---|
| 253 | }
|
|---|
| 254 | else
|
|---|
| 255 | {
|
|---|
| 256 | strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 3);
|
|---|
| 257 | strcat(szBackupWin32Filename, "k");
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | rc = DosMove(pszWin32Filename, szBackupWin32Filename);
|
|---|
| 261 | if (rc != NO_ERROR)
|
|---|
| 262 | {
|
|---|
| 263 | printf("Fatal error: Failed to rename the Win32-file, %s -> %s\n",
|
|---|
| 264 | pszWin32Filename, szBackupWin32Filename);
|
|---|
| 265 | return 81;
|
|---|
| 266 | }
|
|---|
| 267 | printInf(("Backuped %s to %s\n", pszWin32Filename, szBackupWin32Filename));
|
|---|
| 268 |
|
|---|
| 269 | /* switch name */
|
|---|
| 270 | pszOdin32Filename = pszWin32Filename;
|
|---|
| 271 | pszWin32Filename = szBackupWin32Filename;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /* open input file */
|
|---|
| 275 | HFILE hFileWin32;
|
|---|
| 276 |
|
|---|
| 277 | rc = DosOpen(pszWin32Filename, &hFileWin32, &ulAction, 0UL,
|
|---|
| 278 | FILE_NORMAL,
|
|---|
| 279 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
|---|
| 280 | OPEN_FLAGS_RANDOMSEQUENTIAL | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
|---|
| 281 | NULL);
|
|---|
| 282 | if (rc == NO_ERROR)
|
|---|
| 283 | {
|
|---|
| 284 | Pe2Lx pe2lx(hFileWin32);
|
|---|
| 285 | rc = pe2lx.init(pszOdin32Filename);
|
|---|
| 286 | if (rc == NO_ERROR)
|
|---|
| 287 | {
|
|---|
| 288 | rc = pe2lx.writeFile(pszOdin32Filename);
|
|---|
| 289 | if (rc != NO_ERROR)
|
|---|
| 290 | {
|
|---|
| 291 | printf("Fatal error: Failed to write the Lx-file. rc=%d\n", rc);
|
|---|
| 292 | rc = 84;
|
|---|
| 293 | }
|
|---|
| 294 | pe2lx.dumpVirtualLxFile();
|
|---|
| 295 | }
|
|---|
| 296 | else
|
|---|
| 297 | {
|
|---|
| 298 | printf("Fatal error: Failed to convertert the file. rc=%d\n", rc);
|
|---|
| 299 | rc = 83;
|
|---|
| 300 | }
|
|---|
| 301 | DosClose(hFileWin32);
|
|---|
| 302 | }
|
|---|
| 303 | else
|
|---|
| 304 | {
|
|---|
| 305 | printf("Fatal error: Failed to open Win32-file, %s. rc=%d\n",
|
|---|
| 306 | pszWin32Filename, rc);
|
|---|
| 307 | rc = 82;
|
|---|
| 308 | }
|
|---|
| 309 | return (int)rc;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 | /**
|
|---|
| 314 | * Display syntax for this program.
|
|---|
| 315 | * @status completely implemented.
|
|---|
| 316 | * @author knut st. osmundsen
|
|---|
| 317 | */
|
|---|
| 318 | static void syntax()
|
|---|
| 319 | {
|
|---|
| 320 | printf("Syntax: pe2lx.exe [-W<0|1|2|3>] [-1<+|-|[*]>] <Win32File> [Odin32File]\n"
|
|---|
| 321 | "\n"
|
|---|
| 322 | " -W<0|1|2|3|4> Message filter level.\n"
|
|---|
| 323 | " -W0: Output only severe and unrecoverable error messages.\n"
|
|---|
| 324 | " -W1: Output error, severe and unrecoverable error messages.\n"
|
|---|
| 325 | " -W2: Output warning, error, severe and unrecoverable error\n"
|
|---|
| 326 | " messages.\n"
|
|---|
| 327 | " -W3: Output nearly all messages.\n"
|
|---|
| 328 | " -W4: Output absolutely all messages.\n"
|
|---|
| 329 | " Default: -W3\n"
|
|---|
| 330 | " -1<+|-|[*]> All-in-one-object fix.\n"
|
|---|
| 331 | " +: Fix applied when necessary.\n"
|
|---|
| 332 | " -: Disabled. Never applied.\n"
|
|---|
| 333 | " *: Forced. Applied every time.\n"
|
|---|
| 334 | " Default: -1*\n"
|
|---|
| 335 | " -rw Make all segments writable. For use with -1+. A trick to make\n"
|
|---|
| 336 | " it possible for OS/2 to load the objects following on another.\n"
|
|---|
| 337 | " This of course doesn't solve the alignment difference. So if\n"
|
|---|
| 338 | " you build the program pass /ALIGN:0x10000 to the linker.\n"
|
|---|
| 339 | " -f Strip fixups forcing. Don't use with DLLs, may cause traps.\n"
|
|---|
| 340 | "\n"
|
|---|
| 341 | " Custombuild options:\n"
|
|---|
| 342 | " -o:<ordfile> Ordinal file. form: <W32DLL>.<name/ord> @<CustDLLOrd>\n"
|
|---|
| 343 | " -c:<custdll> Custombuild dll. After -o:!\n"
|
|---|
| 344 | " -x:<dll> Exclude from custombuild. -x:MSVCRT for example.\n"
|
|---|
| 345 | "\n"
|
|---|
| 346 | " Win32File Input Win32 Exe, Dll or other Win32 PE file.\n"
|
|---|
| 347 | " Odin32File Output Odin32-file. If not specified the Win32-file is\n"
|
|---|
| 348 | " renamed and the Odin32-file will use the original name\n"
|
|---|
| 349 | " of the Win32-file.\n"
|
|---|
| 350 | " Pe2Lx version 0.%02d\n",
|
|---|
| 351 | PE2LX_VERSION
|
|---|
| 352 | );
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 | #if 0
|
|---|
| 358 | /**
|
|---|
| 359 | * Debug - see how much of the stack that have been used.
|
|---|
| 360 | * Padd stack, and look in the debug storage view on program end.
|
|---|
| 361 | * @param
|
|---|
| 362 | * @status
|
|---|
| 363 | * @author knut st. osmundsen
|
|---|
| 364 | */
|
|---|
| 365 | static void initStack()
|
|---|
| 366 | {
|
|---|
| 367 | PTIB pTib;
|
|---|
| 368 | PPIB pPib;
|
|---|
| 369 |
|
|---|
| 370 | DosGetInfoBlocks(&pTib, &pPib);
|
|---|
| 371 | memset((PVOID)pTib->tib_pstack, 'k', (size_t)&pTib - 0x30 - (size_t)pTib->tib_pstack);
|
|---|
| 372 | }
|
|---|
| 373 | #endif
|
|---|