source: trunk/src/win32k/ldr/xx2lxmain.cpp

Last change on this file was 10394, checked in by sandervl, 22 years ago

KSO: Updates

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