[8247] | 1 | /* $Id: kHllMain.cpp,v 1.2 2002-04-12 01:40:21 bird Exp $
|
---|
[4130] | 2 | *
|
---|
| 3 | * kHllMain - interface to the kHll class.
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
| 6 | *
|
---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | /*******************************************************************************
|
---|
| 13 | * Defined Constants And Macros *
|
---|
| 14 | *******************************************************************************/
|
---|
| 15 | #define INCL_TYPES
|
---|
| 16 | #define INCL_DOSERRORS
|
---|
| 17 | #define FOR_EXEHDR 1 /* exe386.h flag */
|
---|
| 18 | #define DWORD ULONG /* Used by exe386.h / newexe.h */
|
---|
| 19 | #define WORD USHORT /* Used by exe386.h / newexe.h */
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | /*******************************************************************************
|
---|
| 23 | * Internal Functions *
|
---|
| 24 | *******************************************************************************/
|
---|
| 25 | #include <os2.h>
|
---|
| 26 | #include <newexe.h>
|
---|
| 27 | #include <exe386.h>
|
---|
| 28 |
|
---|
| 29 | #include <malloc.h>
|
---|
| 30 | #define free(a) memset(a, '7', _msize(a)) //overload free for debugging purposes.
|
---|
| 31 | #define malloc(a) memset(malloc(a), '3', a) //overload free for debugging purposes.
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 | #include <string.h>
|
---|
| 34 | #include <stddef.h>
|
---|
| 35 | #include <stdlib.h>
|
---|
| 36 | #include <assert.h>
|
---|
| 37 |
|
---|
[8247] | 38 | #include <kTypes.h>
|
---|
| 39 | #include <kError.h>
|
---|
| 40 | #include <kFileInterfaces.h>
|
---|
[4130] | 41 | #include <kList.h>
|
---|
| 42 | #include <kFile.h>
|
---|
| 43 | #include <kFileFormatBase.h>
|
---|
| 44 | #include <kFileLX.h>
|
---|
| 45 |
|
---|
| 46 | #include "hll.h"
|
---|
| 47 | #include "sym.h"
|
---|
| 48 | #include "kHll.h"
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | /*******************************************************************************
|
---|
| 52 | * Internal Functions *
|
---|
| 53 | *******************************************************************************/
|
---|
| 54 | void syntax(void);
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Debugging entry point for the readLX constructor.
|
---|
| 59 | * It reads this executable and dumps it.
|
---|
| 60 | * @param argc Argument count.
|
---|
| 61 | * @param argv Argument vector - only the first entry is used.
|
---|
| 62 | */
|
---|
| 63 | int main(int argc, char **argv)
|
---|
| 64 | {
|
---|
| 65 | kHll * pHll;
|
---|
| 66 | char * pszInput;
|
---|
| 67 | int argi;
|
---|
| 68 | APIRET rc;
|
---|
| 69 |
|
---|
| 70 | /*
|
---|
| 71 | * Check argument count.
|
---|
| 72 | */
|
---|
| 73 | if (argc <= 4)
|
---|
| 74 | {
|
---|
| 75 | syntax();
|
---|
| 76 | return -1;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | /*
|
---|
| 81 | * Read input.
|
---|
| 82 | */
|
---|
| 83 | pszInput = argv[2];
|
---|
| 84 | argi = 3;
|
---|
| 85 | switch (argv[1][0])
|
---|
| 86 | {
|
---|
| 87 | case 'l':
|
---|
| 88 | case 'L':
|
---|
| 89 | pHll = kHll::readLX(pszInput);
|
---|
| 90 | break;
|
---|
| 91 |
|
---|
| 92 | case 'e':
|
---|
| 93 | case 'E':
|
---|
| 94 | try
|
---|
| 95 | {
|
---|
| 96 | kFileLX FileLX(pszInput);
|
---|
| 97 | pHll = kHll::readLXExports(&FileLX);
|
---|
| 98 | }
|
---|
| 99 | catch (int err)
|
---|
| 100 | {
|
---|
| 101 | printf("Failed to read LX file %s (exports).(err=%d)\n",
|
---|
| 102 | pszInput, argv[argi], err);
|
---|
| 103 | return -2;
|
---|
| 104 | }
|
---|
| 105 | break;
|
---|
| 106 |
|
---|
| 107 | case 's':
|
---|
| 108 | case 'S':
|
---|
| 109 | try
|
---|
| 110 | {
|
---|
| 111 | kFile File(pszInput);
|
---|
| 112 | kFileLX FileLX(argv[argi]);
|
---|
| 113 | pHll = kHll::readSym(&File, &FileLX);
|
---|
| 114 | }
|
---|
| 115 | catch (int err)
|
---|
| 116 | {
|
---|
| 117 | printf("Failed to read sym file %s or LX reference file %s.(err=%d)\n",
|
---|
| 118 | pszInput, argv[argi], err);
|
---|
| 119 | return -2;
|
---|
| 120 | }
|
---|
| 121 | argi++;
|
---|
| 122 | break;
|
---|
| 123 |
|
---|
| 124 | default:
|
---|
| 125 | printf("fatal error: invalid input type '%s'\n", argv[1]);
|
---|
| 126 | return -2;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /* Check that read was successfull. */
|
---|
| 130 | if (pHll != NULL)
|
---|
| 131 | printf("Successfully read %s\n", pszInput);
|
---|
| 132 | else
|
---|
| 133 | {
|
---|
| 134 | printf("fatal error: failed to read input\n");
|
---|
| 135 | return -4;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | /*
|
---|
| 140 | * produce output
|
---|
| 141 | */
|
---|
| 142 | for (argi = argi; argi + 1 < argc; argi += 2)
|
---|
| 143 | {
|
---|
| 144 | char *pszOutput = argv[argi+1];
|
---|
| 145 | switch (argv[argi][0])
|
---|
| 146 | {
|
---|
| 147 | case 'd':
|
---|
| 148 | case 'D':
|
---|
| 149 | {
|
---|
| 150 | FILE *ph = fopen(pszOutput, "w");
|
---|
| 151 | if (ph != NULL)
|
---|
| 152 | {
|
---|
| 153 | pHll->dump(ph);
|
---|
| 154 | fclose(ph);
|
---|
| 155 | printf("Successfully wrote dump file %s\n", pszOutput);
|
---|
| 156 | }
|
---|
| 157 | else
|
---|
| 158 | printf("error: failed to open dump file\n");
|
---|
| 159 | break;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | case 'h':
|
---|
| 163 | case 'H':
|
---|
| 164 | {
|
---|
| 165 | if (pHll->write(pszOutput))
|
---|
| 166 | printf("Successfully wrote raw HLL file %s\n", pszOutput);
|
---|
| 167 | else
|
---|
| 168 | printf("Failed writing raw HLL file %s\n", pszOutput);
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | case 'i':
|
---|
| 173 | case 'I':
|
---|
| 174 | try
|
---|
| 175 | {
|
---|
| 176 | kFile kidc(pszOutput, FALSE);
|
---|
| 177 | pHll->ida(&kidc);
|
---|
| 178 | printf("Successfully wrote IDC file %s\n", pszOutput);
|
---|
| 179 | }
|
---|
| 180 | catch (int err)
|
---|
| 181 | {
|
---|
| 182 | printf("Failed writing IDC file %s. err=%d\n", pszOutput, err);
|
---|
| 183 | }
|
---|
| 184 | break;
|
---|
| 185 |
|
---|
| 186 | case 'l':
|
---|
| 187 | case 'L':
|
---|
| 188 | {
|
---|
| 189 | if ((rc = pHll->writeToLX(pszOutput)) == NO_ERROR)
|
---|
| 190 | printf("Successfully wrote to LX file %s\n", pszOutput);
|
---|
| 191 | else
|
---|
| 192 | printf("Failed writing to LX file %s (rc=%d)", pszOutput, rc);
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | case 's':
|
---|
| 197 | case 'S':
|
---|
| 198 | try
|
---|
| 199 | {
|
---|
| 200 | kFile ksym(pszOutput, FALSE);
|
---|
| 201 | pHll->writeSym(&ksym);
|
---|
| 202 | printf("Successfully wrote symbol file %s\n", pszOutput);
|
---|
| 203 | }
|
---|
| 204 | catch (int err)
|
---|
| 205 | {
|
---|
| 206 | printf("Failed writing symbol file %s. err=%d\n", pszOutput, err);
|
---|
| 207 | }
|
---|
| 208 | break;
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 | default:
|
---|
| 212 | printf("error: invalid output type '%s'\n", argv[argi]);
|
---|
| 213 | return -2;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | /* cleanup */
|
---|
| 219 | delete (pHll);
|
---|
| 220 |
|
---|
| 221 | return 0;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | /**
|
---|
| 226 | * Writes program syntax help.
|
---|
| 227 | */
|
---|
| 228 | void syntax(void)
|
---|
| 229 | {
|
---|
| 230 | printf(
|
---|
| 231 | "Syntax: kHll.exe <inputtype> <inputfile> [reffile] <outputtype> <outputfile>\n"
|
---|
| 232 | " (Multiple output pairs are allowed.)\n"
|
---|
| 233 | "\n"
|
---|
| 234 | " InputType:\n"
|
---|
| 235 | " l LX (HLL) debuginfo.\n"
|
---|
| 236 | " e LX export table.\n"
|
---|
| 237 | " s Symbol files with extra LX reference file [reffile].\n"
|
---|
| 238 | "\n"
|
---|
| 239 | " OutputType:\n"
|
---|
| 240 | " i IDA IDC macro file.\n"
|
---|
| 241 | " h Raw HLL file.\n"
|
---|
| 242 | " l Add as debuginfo to an LX executable.\n"
|
---|
| 243 | " s Symbol file (.SYM).\n"
|
---|
| 244 | " d Dump.\n"
|
---|
| 245 | " \n"
|
---|
| 246 | "(Copyright (c) 2000 knut st. osmundsen)\n"
|
---|
| 247 | );
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | #ifdef __IBMCPP__
|
---|
| 251 | #include "klist.cpp"
|
---|
| 252 | #endif
|
---|
| 253 |
|
---|