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