H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 1 | /* The Netwide Assembler main program module |
| 2 | * |
| 3 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and |
| 4 | * Julian Hall. All rights reserved. The software is |
| 5 | * redistributable under the licence given in the file "Licence" |
| 6 | * distributed in the NASM archive. |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <ctype.h> |
| 14 | |
| 15 | #include "nasm.h" |
| 16 | #include "nasmlib.h" |
| 17 | #include "parser.h" |
| 18 | #include "assemble.h" |
| 19 | #include "labels.h" |
| 20 | #include "outform.h" |
| 21 | |
| 22 | static void report_error (int, char *, ...); |
| 23 | static void parse_cmdline (int, char **); |
| 24 | static void assemble_file (char *); |
| 25 | static int getkw (char *buf, char **value); |
| 26 | static void register_output_formats(void); |
| 27 | static void usage(void); |
| 28 | |
| 29 | static char *obuf; |
| 30 | static char inname[FILENAME_MAX]; |
| 31 | static char outname[FILENAME_MAX]; |
| 32 | static char realout[FILENAME_MAX]; |
| 33 | static int lineno; /* for error reporting */ |
| 34 | static int pass; |
| 35 | static struct ofmt *ofmt = NULL; |
| 36 | |
| 37 | static FILE *ofile = NULL; |
| 38 | static int sb = 16; /* by default */ |
| 39 | |
| 40 | static long current_seg; |
| 41 | static struct RAA *offsets; |
| 42 | static long abs_offset; |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 43 | |
| 44 | static struct SAA *forwrefs; /* keep track of forward references */ |
| 45 | static int forwline; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * get/set current offset... |
| 49 | */ |
| 50 | #define get_curr_ofs (current_seg==NO_SEG?abs_offset:\ |
| 51 | raa_read(offsets,current_seg)) |
| 52 | #define set_curr_ofs(x) (current_seg==NO_SEG?(void)(abs_offset=(x)):\ |
| 53 | (void)(offsets=raa_write(offsets,current_seg,(x)))) |
| 54 | |
| 55 | static int want_usage; |
| 56 | static int terminate_after_phase; |
| 57 | |
| 58 | int main(int argc, char **argv) { |
| 59 | want_usage = terminate_after_phase = FALSE; |
| 60 | |
| 61 | nasm_set_malloc_error (report_error); |
| 62 | offsets = raa_init(); |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 63 | forwrefs = saa_init ((long)sizeof(int)); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 64 | |
| 65 | seg_init(); |
| 66 | |
| 67 | register_output_formats(); |
| 68 | |
| 69 | parse_cmdline(argc, argv); |
| 70 | |
| 71 | if (terminate_after_phase) { |
| 72 | if (want_usage) |
| 73 | usage(); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | if (!*outname) { |
| 78 | ofmt->filename (inname, realout, report_error); |
| 79 | strcpy(outname, realout); |
| 80 | } |
| 81 | |
| 82 | ofile = fopen(outname, "wb"); |
| 83 | if (!ofile) { |
| 84 | report_error (ERR_FATAL | ERR_NOFILE, |
| 85 | "unable to open output file `%s'", outname); |
| 86 | } |
| 87 | ofmt->init (ofile, report_error, define_label); |
| 88 | assemble_file (inname); |
| 89 | if (!terminate_after_phase) { |
| 90 | ofmt->cleanup (); |
| 91 | cleanup_labels (); |
| 92 | } |
| 93 | fclose (ofile); |
| 94 | if (terminate_after_phase) |
| 95 | remove(outname); |
| 96 | |
| 97 | if (want_usage) |
| 98 | usage(); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static void parse_cmdline(int argc, char **argv) { |
| 104 | char *param; |
| 105 | |
| 106 | *inname = *outname = '\0'; |
| 107 | while (--argc) { |
| 108 | char *p = *++argv; |
| 109 | if (p[0]=='-') { |
| 110 | switch (p[1]) { |
| 111 | case 'o': /* these parameters take values */ |
| 112 | case 'f': |
| 113 | if (p[2]) /* the parameter's in the option */ |
| 114 | param = p+2; |
| 115 | else if (!argv[1]) { |
| 116 | report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE, |
| 117 | "option `-%c' requires an argument", |
| 118 | p[1]); |
| 119 | break; |
| 120 | } else |
| 121 | --argc, param = *++argv; |
| 122 | if (p[1]=='o') { /* output file */ |
| 123 | strcpy (outname, param); |
| 124 | } else if (p[1]=='f') { /* output format */ |
| 125 | ofmt = ofmt_find(param); |
| 126 | if (!ofmt) { |
| 127 | report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE, |
| 128 | "unrecognised output format `%s'", |
| 129 | param); |
| 130 | } |
| 131 | } |
| 132 | break; |
| 133 | case 'h': |
| 134 | fprintf(stderr, |
| 135 | "usage: nasm [-o outfile] [-f format] filename\n"); |
| 136 | fprintf(stderr, |
| 137 | " or nasm -r for version info\n\n"); |
| 138 | fprintf(stderr, |
| 139 | "valid output formats for -f are" |
| 140 | " (`*' denotes default):\n"); |
| 141 | ofmt_list(ofmt); |
| 142 | exit (0); /* never need usage message here */ |
| 143 | break; |
| 144 | case 'r': |
| 145 | fprintf(stderr, "NASM version %s\n", NASM_VER); |
| 146 | exit (0); /* never need usage message here */ |
| 147 | break; |
| 148 | default: |
| 149 | report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE, |
| 150 | "unrecognised option `-%c'", |
| 151 | p[1]); |
| 152 | break; |
| 153 | } |
| 154 | } else { |
| 155 | if (*inname) { |
| 156 | report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE, |
| 157 | "more than one input file specified"); |
| 158 | } else |
| 159 | strcpy(inname, p); |
| 160 | } |
| 161 | } |
| 162 | if (!*inname) |
| 163 | report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE, |
| 164 | "no input file specified"); |
| 165 | } |
| 166 | |
| 167 | /* used by error function to report location */ |
| 168 | static char currentfile[FILENAME_MAX]; |
| 169 | |
| 170 | static void assemble_file (char *fname) { |
| 171 | FILE *fp = fopen (fname, "r"); |
| 172 | FILE *oldfile = NULL; /* jrh - used when processing include files */ |
| 173 | int oldfileline = 0; |
| 174 | char *value, *p, buffer[1024+2]; /* maximum line length defined here */ |
| 175 | insn output_ins; |
| 176 | int i, seg, rn_error; |
| 177 | |
| 178 | if (!fp) { /* couldn't open file */ |
| 179 | report_error (ERR_FATAL | ERR_NOFILE, |
| 180 | "unable to open input file `%s'", fname); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | init_labels (); |
| 185 | strcpy(currentfile,fname); |
| 186 | |
| 187 | /* pass one */ |
| 188 | pass = 1; |
| 189 | current_seg = ofmt->section(NULL, pass, &sb); |
| 190 | lineno = 0; |
| 191 | while (1) { |
| 192 | if (! fgets(buffer, sizeof(buffer), fp)) { /* EOF on current file */ |
| 193 | if (oldfile) { |
| 194 | fclose(fp); |
| 195 | fp = oldfile; |
| 196 | lineno = oldfileline; |
| 197 | strcpy(currentfile,fname); |
| 198 | oldfile = NULL; |
| 199 | continue; |
| 200 | } |
| 201 | else |
| 202 | break; |
| 203 | } |
| 204 | lineno++; |
| 205 | if (buffer[strlen(buffer)-1] == '\n') { |
| 206 | buffer[strlen(buffer)-1] = '\0'; |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 207 | } else if (!feof(fp)) { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 208 | /* |
| 209 | * We have a line that's too long. Throw an error, read |
| 210 | * to EOL, and ignore the line for assembly purposes. |
| 211 | */ |
| 212 | report_error (ERR_NONFATAL, "line is longer than %d characters", |
| 213 | sizeof(buffer)-2); |
| 214 | while (fgets(buffer, sizeof(buffer), fp) && |
| 215 | buffer[strlen(buffer)-1] != '\n'); |
| 216 | continue; /* read another line */ |
| 217 | } |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 218 | /* |
| 219 | * Handle spurious ^Z, which may be inserted by some file |
| 220 | * transfer utilities. |
| 221 | */ |
| 222 | buffer[strcspn(buffer, "\032")] = '\0'; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 223 | |
| 224 | /* here we parse our directives; this is not handled by the 'real' |
| 225 | * parser. */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 226 | if ( (i = getkw (buffer, &value)) ) { |
| 227 | switch (i) { |
| 228 | case 1: /* [SEGMENT n] */ |
| 229 | seg = ofmt->section (value, pass, &sb); |
| 230 | if (seg == NO_SEG) { |
| 231 | report_error (ERR_NONFATAL, |
| 232 | "segment name `%s' not recognised", |
| 233 | value); |
| 234 | } else { |
| 235 | current_seg = seg; |
| 236 | } |
| 237 | break; |
| 238 | case 2: /* [EXTERN label] */ |
| 239 | if (*value == '$') |
| 240 | value++; /* skip initial $ if present */ |
| 241 | declare_as_global (value, report_error); |
| 242 | define_label (value, seg_alloc(), 0L, ofmt, report_error); |
| 243 | break; |
| 244 | case 3: /* [BITS bits] */ |
| 245 | switch (atoi(value)) { |
| 246 | case 16: |
| 247 | case 32: |
| 248 | sb = atoi(value); |
| 249 | break; |
| 250 | default: |
| 251 | report_error(ERR_NONFATAL, |
| 252 | "`%s' is not a valid argument to [BITS]", |
| 253 | value); |
| 254 | break; |
| 255 | } |
| 256 | break; |
| 257 | case 4: /* [INC file] */ |
| 258 | oldfile = fp; |
| 259 | oldfileline = lineno; |
| 260 | lineno = 0; |
| 261 | strcpy(currentfile,value); |
| 262 | fp = fopen(value,"r"); |
| 263 | if (!fp) { |
| 264 | lineno = oldfileline; |
| 265 | fp = oldfile; |
| 266 | strcpy(currentfile,fname); |
| 267 | report_error (ERR_FATAL, |
| 268 | "unable to open include file `%s'\n", |
| 269 | value); |
| 270 | } |
| 271 | break; |
| 272 | case 5: /* [GLOBAL symbol] */ |
| 273 | if (*value == '$') |
| 274 | value++; /* skip initial $ if present */ |
| 275 | declare_as_global (value, report_error); |
| 276 | break; |
| 277 | case 6: /* [COMMON symbol size] */ |
| 278 | p = value; |
| 279 | while (*p && !isspace(*p)) |
| 280 | p++; |
| 281 | if (*p) { |
| 282 | long size; |
| 283 | |
| 284 | while (*p && isspace(*p)) |
| 285 | *p++ = '\0'; |
| 286 | size = readnum (p, &rn_error); |
| 287 | if (rn_error) |
| 288 | report_error (ERR_NONFATAL, "invalid size specified" |
| 289 | " in COMMON declaration"); |
| 290 | else |
| 291 | define_common (value, seg_alloc(), size, |
| 292 | ofmt, report_error); |
| 293 | } else |
| 294 | report_error (ERR_NONFATAL, "no size specified in" |
| 295 | " COMMON declaration"); |
| 296 | break; |
| 297 | case 7: /* [ABSOLUTE address] */ |
| 298 | current_seg = NO_SEG; |
| 299 | abs_offset = readnum(value, &rn_error); |
| 300 | if (rn_error) { |
| 301 | report_error (ERR_NONFATAL, "invalid address specified" |
| 302 | " for ABSOLUTE directive"); |
| 303 | abs_offset = 0x100;/* don't go near zero in case of / */ |
| 304 | } |
| 305 | break; |
| 306 | default: |
| 307 | if (!ofmt->directive (buffer+1, value, 1)) |
| 308 | report_error (ERR_NONFATAL, "unrecognised directive [%s]", |
| 309 | buffer+1); |
| 310 | break; |
| 311 | } |
| 312 | } else { |
| 313 | long offs = get_curr_ofs; |
| 314 | parse_line (current_seg, offs, lookup_label, |
| 315 | 1, buffer, &output_ins, ofmt, report_error); |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 316 | if (output_ins.forw_ref) |
| 317 | *(int *)saa_wstruct(forwrefs) = lineno; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 318 | if (output_ins.opcode == I_EQU) { |
| 319 | /* |
| 320 | * Special `..' EQUs get processed in pass two. |
| 321 | */ |
| 322 | if (!output_ins.label) |
| 323 | report_error (ERR_NONFATAL, |
| 324 | "EQU not preceded by label"); |
| 325 | else if (output_ins.label[0] != '.' || |
| 326 | output_ins.label[1] != '.') { |
| 327 | if (output_ins.operands == 1 && |
| 328 | (output_ins.oprs[0].type & IMMEDIATE)) { |
| 329 | define_label (output_ins.label, |
| 330 | output_ins.oprs[0].segment, |
| 331 | output_ins.oprs[0].offset, |
| 332 | ofmt, report_error); |
| 333 | } else if (output_ins.operands == 2 && |
| 334 | (output_ins.oprs[0].type & IMMEDIATE) && |
| 335 | (output_ins.oprs[0].type & COLON) && |
| 336 | output_ins.oprs[0].segment == NO_SEG && |
| 337 | (output_ins.oprs[1].type & IMMEDIATE) && |
| 338 | output_ins.oprs[1].segment == NO_SEG) { |
| 339 | define_label (output_ins.label, |
| 340 | output_ins.oprs[0].offset | SEG_ABS, |
| 341 | output_ins.oprs[1].offset, |
| 342 | ofmt, report_error); |
| 343 | } else |
| 344 | report_error(ERR_NONFATAL, "bad syntax for EQU"); |
| 345 | } |
| 346 | } else { |
| 347 | if (output_ins.label) |
| 348 | define_label (output_ins.label, |
| 349 | current_seg, offs, |
| 350 | ofmt, report_error); |
| 351 | offs += insn_size (current_seg, offs, sb, |
| 352 | &output_ins, report_error); |
| 353 | set_curr_ofs (offs); |
| 354 | } |
| 355 | cleanup_insn (&output_ins); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (terminate_after_phase) { |
| 360 | fclose(ofile); |
| 361 | remove(outname); |
| 362 | if (want_usage) |
| 363 | usage(); |
| 364 | exit (1); |
| 365 | } |
| 366 | |
| 367 | /* pass two */ |
| 368 | pass = 2; |
| 369 | rewind (fp); |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 370 | saa_rewind (forwrefs); |
| 371 | { |
| 372 | int *p = saa_rstruct (forwrefs); |
| 373 | if (p) |
| 374 | forwline = *p; |
| 375 | else |
| 376 | forwline = -1; |
| 377 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 378 | current_seg = ofmt->section(NULL, pass, &sb); |
| 379 | raa_free (offsets); |
| 380 | offsets = raa_init(); |
| 381 | lineno = 0; |
| 382 | while (1) { |
| 383 | if (!fgets(buffer, sizeof(buffer), fp)) { |
| 384 | if (oldfile) { |
| 385 | fclose(fp); |
| 386 | fp = oldfile; |
| 387 | lineno = oldfileline; |
| 388 | strcpy(currentfile,fname); |
| 389 | oldfile = NULL; |
| 390 | continue; |
| 391 | } else |
| 392 | break; |
| 393 | } |
| 394 | lineno++; |
| 395 | if (buffer[strlen(buffer)-1] == '\n') |
| 396 | buffer[strlen(buffer)-1] = '\0'; |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 397 | else if (!feof(fp)) |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 398 | report_error (ERR_PANIC, |
| 399 | "too-long line got through from pass one"); |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 400 | /* |
| 401 | * Handle spurious ^Z, which may be inserted by some file |
| 402 | * transfer utilities. |
| 403 | */ |
| 404 | buffer[strcspn(buffer, "\032")] = '\0'; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 405 | |
| 406 | /* here we parse our directives; this is not handled by |
| 407 | * the 'real' parser. */ |
| 408 | |
| 409 | if ( (i = getkw (buffer, &value)) ) { |
| 410 | switch (i) { |
| 411 | case 1: /* [SEGMENT n] */ |
| 412 | seg = ofmt->section (value, pass, &sb); |
| 413 | if (seg == NO_SEG) { |
| 414 | report_error (ERR_PANIC, |
| 415 | "invalid segment name on pass two"); |
| 416 | } else |
| 417 | current_seg = seg; |
| 418 | break; |
| 419 | case 2: /* [EXTERN label] */ |
| 420 | break; |
| 421 | case 3: /* [BITS bits] */ |
| 422 | switch (atoi(value)) { |
| 423 | case 16: |
| 424 | case 32: |
| 425 | sb = atoi(value); |
| 426 | break; |
| 427 | default: |
| 428 | report_error(ERR_PANIC, |
| 429 | "invalid [BITS] value on pass two", |
| 430 | value); |
| 431 | break; |
| 432 | } |
| 433 | break; |
| 434 | case 4: |
| 435 | oldfile = fp; |
| 436 | oldfileline = lineno; |
| 437 | lineno = 0; |
| 438 | strcpy(currentfile,value); |
| 439 | fp = fopen(value,"r"); |
| 440 | if (!fp) { |
| 441 | lineno = oldfileline; |
| 442 | fp = oldfile; |
| 443 | strcpy(currentfile,fname); |
| 444 | /* |
| 445 | * We don't report this error in the PANIC |
| 446 | * class, even though we might expect to have |
| 447 | * already picked it up during pass one, |
| 448 | * because of the tiny chance that some other |
| 449 | * process may have removed the include file |
| 450 | * between the passes. |
| 451 | */ |
| 452 | report_error (ERR_FATAL, |
| 453 | "unable to open include file `%s'\n", |
| 454 | value); |
| 455 | } |
| 456 | break; |
| 457 | case 5: /* [GLOBAL symbol] */ |
| 458 | break; |
| 459 | case 6: /* [COMMON symbol size] */ |
| 460 | break; |
| 461 | case 7: /* [ABSOLUTE addr] */ |
| 462 | current_seg = NO_SEG; |
| 463 | abs_offset = readnum(value, &rn_error); |
| 464 | if (rn_error) |
| 465 | report_error (ERR_PANIC, "invalid ABSOLUTE address " |
| 466 | "in pass two"); |
| 467 | break; |
| 468 | default: |
| 469 | if (!ofmt->directive (buffer+1, value, 2)) |
| 470 | report_error (ERR_PANIC, "invalid directive on pass two"); |
| 471 | break; |
| 472 | } |
| 473 | } else { |
| 474 | long offs = get_curr_ofs; |
| 475 | parse_line (current_seg, offs, lookup_label, 2, |
| 476 | buffer, &output_ins, ofmt, report_error); |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame^] | 477 | if (lineno == forwline) { |
| 478 | int *p = saa_rstruct (forwrefs); |
| 479 | if (p) |
| 480 | forwline = *p; |
| 481 | else |
| 482 | forwline = -1; |
| 483 | output_ins.forw_ref = TRUE; |
| 484 | } else |
| 485 | output_ins.forw_ref = FALSE; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 486 | obuf = buffer; |
| 487 | if (output_ins.label) |
| 488 | define_label_stub (output_ins.label, report_error); |
| 489 | if (output_ins.opcode == I_EQU) { |
| 490 | /* |
| 491 | * Special `..' EQUs get processed here. |
| 492 | */ |
| 493 | if (output_ins.label[0] == '.' && |
| 494 | output_ins.label[1] == '.') { |
| 495 | if (output_ins.operands == 1 && |
| 496 | (output_ins.oprs[0].type & IMMEDIATE)) { |
| 497 | define_label (output_ins.label, |
| 498 | output_ins.oprs[0].segment, |
| 499 | output_ins.oprs[0].offset, |
| 500 | ofmt, report_error); |
| 501 | } else if (output_ins.operands == 2 && |
| 502 | (output_ins.oprs[0].type & IMMEDIATE) && |
| 503 | (output_ins.oprs[0].type & COLON) && |
| 504 | output_ins.oprs[0].segment == NO_SEG && |
| 505 | (output_ins.oprs[1].type & IMMEDIATE) && |
| 506 | output_ins.oprs[1].segment == NO_SEG) { |
| 507 | define_label (output_ins.label, |
| 508 | output_ins.oprs[0].offset | SEG_ABS, |
| 509 | output_ins.oprs[1].offset, |
| 510 | ofmt, report_error); |
| 511 | } else |
| 512 | report_error(ERR_NONFATAL, "bad syntax for EQU"); |
| 513 | } |
| 514 | } |
| 515 | offs += assemble (current_seg, offs, sb, |
| 516 | &output_ins, ofmt, report_error); |
| 517 | cleanup_insn (&output_ins); |
| 518 | set_curr_ofs (offs); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static int getkw (char *buf, char **value) { |
| 524 | char *p, *q; |
| 525 | |
| 526 | if (*buf!='[') |
| 527 | return 0; |
| 528 | p = buf; |
| 529 | while (*p && *p != ']') p++; |
| 530 | if (!*p) |
| 531 | return 0; |
| 532 | q = p++; |
| 533 | while (*p && *p != ';') { |
| 534 | if (!isspace(*p)) |
| 535 | return 0; |
| 536 | p++; |
| 537 | } |
| 538 | q[1] = '\0'; |
| 539 | |
| 540 | p = buf+1; |
| 541 | while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t') |
| 542 | buf++; |
| 543 | if (*buf==']') { |
| 544 | *buf = '\0'; |
| 545 | *value = buf; |
| 546 | } else { |
| 547 | *buf++ = '\0'; |
| 548 | *value = buf; |
| 549 | while (*buf!=']') buf++; |
| 550 | *buf++ = '\0'; |
| 551 | } |
| 552 | for (q=p; *q; q++) |
| 553 | *q = tolower(*q); |
| 554 | if (!strcmp(p, "segment") || !strcmp(p, "section")) |
| 555 | return 1; |
| 556 | if (!strcmp(p, "extern")) |
| 557 | return 2; |
| 558 | if (!strcmp(p, "bits")) |
| 559 | return 3; |
| 560 | if (!strcmp(p, "inc") || !strcmp(p, "include")) |
| 561 | return 4; |
| 562 | if (!strcmp(p, "global")) |
| 563 | return 5; |
| 564 | if (!strcmp(p, "common")) |
| 565 | return 6; |
| 566 | if (!strcmp(p, "absolute")) |
| 567 | return 7; |
| 568 | return -1; |
| 569 | } |
| 570 | |
| 571 | static void report_error (int severity, char *fmt, ...) { |
| 572 | va_list ap; |
| 573 | |
| 574 | if (severity & ERR_NOFILE) |
| 575 | fputs ("nasm: ", stderr); |
| 576 | else |
| 577 | fprintf (stderr, "%s:%d: ", currentfile, lineno); |
| 578 | |
| 579 | if ( (severity & ERR_MASK) == ERR_WARNING) |
| 580 | fputs ("warning: ", stderr); |
| 581 | else if ( (severity & ERR_MASK) == ERR_PANIC) |
| 582 | fputs ("panic: ", stderr); |
| 583 | |
| 584 | va_start (ap, fmt); |
| 585 | vfprintf (stderr, fmt, ap); |
| 586 | fputc ('\n', stderr); |
| 587 | |
| 588 | if (severity & ERR_USAGE) |
| 589 | want_usage = TRUE; |
| 590 | |
| 591 | switch (severity & ERR_MASK) { |
| 592 | case ERR_WARNING: |
| 593 | /* no further action, by definition */ |
| 594 | break; |
| 595 | case ERR_NONFATAL: |
| 596 | terminate_after_phase = TRUE; |
| 597 | break; |
| 598 | case ERR_FATAL: |
| 599 | fclose(ofile); |
| 600 | remove(outname); |
| 601 | if (want_usage) |
| 602 | usage(); |
| 603 | exit(1); /* instantly die */ |
| 604 | break; /* placate silly compilers */ |
| 605 | case ERR_PANIC: |
| 606 | abort(); /* panic and dump core */ |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | static void usage(void) { |
| 612 | fputs("type `nasm -h' for help\n", stderr); |
| 613 | } |
| 614 | |
| 615 | static void register_output_formats(void) { |
| 616 | /* Flat-form binary format */ |
| 617 | #ifdef OF_BIN |
| 618 | extern struct ofmt of_bin; |
| 619 | #endif |
| 620 | /* Unix formats: a.out, COFF, ELF */ |
| 621 | #ifdef OF_AOUT |
| 622 | extern struct ofmt of_aout; |
| 623 | #endif |
| 624 | #ifdef OF_COFF |
| 625 | extern struct ofmt of_coff; |
| 626 | #endif |
| 627 | #ifdef OF_ELF |
| 628 | extern struct ofmt of_elf; |
| 629 | #endif |
| 630 | /* Linux strange format: as86 */ |
| 631 | #ifdef OF_AS86 |
| 632 | extern struct ofmt of_as86; |
| 633 | #endif |
| 634 | /* DOS formats: OBJ, Win32 */ |
| 635 | #ifdef OF_OBJ |
| 636 | extern struct ofmt of_obj; |
| 637 | #endif |
| 638 | #ifdef OF_WIN32 |
| 639 | extern struct ofmt of_win32; |
| 640 | #endif |
| 641 | #ifdef OF_RDF |
| 642 | extern struct ofmt of_rdf; |
| 643 | #endif |
| 644 | #ifdef OF_DBG /* debug format must be included specifically */ |
| 645 | extern struct ofmt of_dbg; |
| 646 | #endif |
| 647 | |
| 648 | #ifdef OF_BIN |
| 649 | ofmt_register (&of_bin); |
| 650 | #endif |
| 651 | #ifdef OF_AOUT |
| 652 | ofmt_register (&of_aout); |
| 653 | #endif |
| 654 | #ifdef OF_COFF |
| 655 | ofmt_register (&of_coff); |
| 656 | #endif |
| 657 | #ifdef OF_ELF |
| 658 | ofmt_register (&of_elf); |
| 659 | #endif |
| 660 | #ifdef OF_AS86 |
| 661 | ofmt_register (&of_as86); |
| 662 | #endif |
| 663 | #ifdef OF_OBJ |
| 664 | ofmt_register (&of_obj); |
| 665 | #endif |
| 666 | #ifdef OF_WIN32 |
| 667 | ofmt_register (&of_win32); |
| 668 | #endif |
| 669 | #ifdef OF_RDF |
| 670 | ofmt_register (&of_rdf); |
| 671 | #endif |
| 672 | #ifdef OF_DBG |
| 673 | ofmt_register (&of_dbg); |
| 674 | #endif |
| 675 | /* |
| 676 | * set the default format |
| 677 | */ |
| 678 | ofmt = &OF_DEFAULT; |
| 679 | } |