source: trunk/src/binutils/gas/as.c@ 618

Last change on this file since 618 was 618, checked in by bird, 22 years ago

Joined the port of 2.11.2 with 2.14.

  • Property cvs2svn:cvs-rev set to 1.3
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 31.3 KB
Line 
1/* as.c - GAS main program.
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23/* Main program for AS; a 32-bit assembler of GNU.
24 * Understands command arguments.
25 * Has a few routines that don't fit in other modules because they
26 * are shared.
27 *
28 * bugs
29 *
30 * : initialisers
31 * Since no-one else says they will support them in future: I
32 * don't support them now.
33 */
34
35#include "ansidecl.h"
36
37#define COMMON
38
39#include "as.h"
40#include "subsegs.h"
41#include "output-file.h"
42#include "sb.h"
43#include "macro.h"
44#include "dwarf2dbg.h"
45
46#ifdef BFD_ASSEMBLER
47#include "bfdver.h"
48#endif
49
50#ifdef HAVE_ITBL_CPU
51#include "itbl-ops.h"
52#else
53#define itbl_parse(itbl_file) 1
54#define itbl_init()
55#endif
56
57#ifdef HAVE_SBRK
58#ifdef NEED_DECLARATION_SBRK
59extern PTR sbrk ();
60#endif
61#endif
62
63static void show_usage PARAMS ((FILE *));
64static void parse_args PARAMS ((int *, char ***));
65static void dump_statistics PARAMS ((void));
66static void perform_an_assembly_pass PARAMS ((int argc, char **argv));
67static int macro_expr PARAMS ((const char *, int, sb *, int *));
68#ifdef USING_CGEN
69/* Perform any cgen specific initialisation for gas. */
70extern void gas_cgen_begin PARAMS ((void));
71#endif
72
73/* True if a listing is wanted. */
74int listing;
75
76/* Name of listing file. */
77static char *listing_filename = NULL;
78
79/* Type of debugging to generate. */
80
81enum debug_info_type debug_type = DEBUG_UNSPECIFIED;
82
83/* Maximum level of macro nesting. */
84int max_macro_nest = 100;
85
86/* argv[0] */
87char *myname;
88#ifdef BFD_ASSEMBLER
89segT reg_section, expr_section;
90segT text_section, data_section, bss_section;
91#endif
92
93/* The default obstack chunk size. If we set this to zero, the
94 obstack code will use whatever will fit in a 4096 byte block. */
95int chunksize = 0;
96
97/* To monitor memory allocation more effectively, make this non-zero.
98 Then the chunk sizes for gas and bfd will be reduced. */
99int debug_memory = 0;
100
101/* We build a list of defsyms as we read the options, and then define
102 them after we have initialized everything. */
103
104struct defsym_list {
105 struct defsym_list *next;
106 char *name;
107 valueT value;
108};
109
110static struct defsym_list *defsyms;
111
112/* Keep a record of the itbl files we read in. */
113
114struct itbl_file_list {
115 struct itbl_file_list *next;
116 char *name;
117};
118
119static struct itbl_file_list *itbl_files;
120
121
122#ifdef EMX
123static char *omf_file_name;
124int emx_omf; /* -Zomf */
125int emx_strip; /* -Zstrip */
126#endif /* EMX */
127
128
129#ifdef USE_EMULATIONS
130#define EMULATION_ENVIRON "AS_EMULATION"
131
132extern struct emulation mipsbelf, mipslelf, mipself;
133extern struct emulation mipsbecoff, mipslecoff, mipsecoff;
134extern struct emulation i386coff, i386elf, i386aout;
135extern struct emulation crisaout, criself;
136
137static struct emulation *const emulations[] = { EMULATIONS };
138static const int n_emulations = sizeof (emulations) / sizeof (emulations[0]);
139
140static void select_emulation_mode PARAMS ((int, char **));
141
142static void
143select_emulation_mode (argc, argv)
144 int argc;
145 char **argv;
146{
147 int i;
148 char *p, *em = 0;
149
150 for (i = 1; i < argc; i++)
151 if (!strncmp ("--em", argv[i], 4))
152 break;
153
154 if (i == argc)
155 goto do_default;
156
157 p = strchr (argv[i], '=');
158 if (p)
159 p++;
160 else
161 p = argv[i + 1];
162
163 if (!p || !*p)
164 as_fatal (_("missing emulation mode name"));
165 em = p;
166
167 do_default:
168 if (em == 0)
169 em = getenv (EMULATION_ENVIRON);
170 if (em == 0)
171 em = DEFAULT_EMULATION;
172
173 if (em)
174 {
175 for (i = 0; i < n_emulations; i++)
176 if (!strcmp (emulations[i]->name, em))
177 break;
178 if (i == n_emulations)
179 as_fatal (_("unrecognized emulation name `%s'"), em);
180 this_emulation = emulations[i];
181 }
182 else
183 this_emulation = emulations[0];
184
185 this_emulation->init ();
186}
187
188const char *
189default_emul_bfd_name ()
190{
191 abort ();
192 return NULL;
193}
194
195void
196common_emul_init ()
197{
198 this_format = this_emulation->format;
199
200 if (this_emulation->leading_underscore == 2)
201 this_emulation->leading_underscore = this_format->dfl_leading_underscore;
202
203 if (this_emulation->default_endian != 2)
204 target_big_endian = this_emulation->default_endian;
205
206 if (this_emulation->fake_label_name == 0)
207 {
208 if (this_emulation->leading_underscore)
209 this_emulation->fake_label_name = "L0\001";
210 else
211 /* What other parameters should we test? */
212 this_emulation->fake_label_name = ".L0\001";
213 }
214}
215#endif
216
217void
218print_version_id ()
219{
220 static int printed;
221 if (printed)
222 return;
223 printed = 1;
224
225#ifdef BFD_ASSEMBLER
226 fprintf (stderr, _("GNU assembler version %s (%s) using BFD version %s"),
227 VERSION, TARGET_ALIAS, BFD_VERSION_STRING);
228#else
229 fprintf (stderr, _("GNU assembler version %s (%s)"), VERSION, TARGET_ALIAS);
230#endif
231 fprintf (stderr, "\n");
232}
233
234static void
235show_usage (stream)
236 FILE *stream;
237{
238 fprintf (stream, _("Usage: %s [option...] [asmfile...]\n"), myname);
239
240 fprintf (stream, _("\
241Options:\n\
242 -a[sub-option...] turn on listings\n\
243 Sub-options [default hls]:\n\
244 c omit false conditionals\n\
245 d omit debugging directives\n\
246 h include high-level source\n\
247 l include assembly\n\
248 m include macro expansions\n\
249 n omit forms processing\n\
250 s include symbols\n\
251 =FILE list to FILE (must be last sub-option)\n"));
252
253 fprintf (stream, _("\
254 -D produce assembler debugging messages\n"));
255 fprintf (stream, _("\
256 --defsym SYM=VAL define symbol SYM to given value\n"));
257#ifdef USE_EMULATIONS
258 {
259 int i;
260 char *def_em;
261
262 fprintf (stream, "\
263 --em=[");
264 for (i = 0; i < n_emulations - 1; i++)
265 fprintf (stream, "%s | ", emulations[i]->name);
266 fprintf (stream, "%s]\n", emulations[i]->name);
267
268 def_em = getenv (EMULATION_ENVIRON);
269 if (!def_em)
270 def_em = DEFAULT_EMULATION;
271 fprintf (stream, _("\
272 emulate output (default %s)\n"), def_em);
273 }
274#endif
275 fprintf (stream, _("\
276 -f skip whitespace and comment preprocessing\n"));
277 fprintf (stream, _("\
278 --gstabs generate stabs debugging information\n"));
279 fprintf (stream, _("\
280 --gdwarf2 generate DWARF2 debugging information\n"));
281 fprintf (stream, _("\
282 --help show this message and exit\n"));
283 fprintf (stream, _("\
284 --target-help show target specific options\n"));
285 fprintf (stream, _("\
286 -I DIR add DIR to search list for .include directives\n"));
287 fprintf (stream, _("\
288 -J don't warn about signed overflow\n"));
289 fprintf (stream, _("\
290 -K warn when differences altered for long displacements\n"));
291 fprintf (stream, _("\
292 -L,--keep-locals keep local symbols (e.g. starting with `L')\n"));
293 fprintf (stream, _("\
294 -M,--mri assemble in MRI compatibility mode\n"));
295 fprintf (stream, _("\
296 --MD FILE write dependency information in FILE (default none)\n"));
297 fprintf (stream, _("\
298 -nocpp ignored\n"));
299 fprintf (stream, _("\
300 -o OBJFILE name the object-file output OBJFILE (default a.out)\n"));
301 fprintf (stream, _("\
302 -R fold data section into text section\n"));
303 fprintf (stream, _("\
304 --statistics print various measured statistics from execution\n"));
305 fprintf (stream, _("\
306 --strip-local-absolute strip local absolute symbols\n"));
307 fprintf (stream, _("\
308 --traditional-format Use same format as native assembler when possible\n"));
309 fprintf (stream, _("\
310 --version print assembler version number and exit\n"));
311 fprintf (stream, _("\
312 -W --no-warn suppress warnings\n"));
313 fprintf (stream, _("\
314 --warn don't suppress warnings\n"));
315 fprintf (stream, _("\
316 --fatal-warnings treat warnings as errors\n"));
317 fprintf (stream, _("\
318 --itbl INSTTBL extend instruction set to include instructions\n\
319 matching the specifications defined in file INSTTBL\n"));
320 fprintf (stream, _("\
321 -w ignored\n"));
322 fprintf (stream, _("\
323 -X ignored\n"));
324 fprintf (stream, _("\
325 -Z generate object file even after errors\n"));
326 fprintf (stream, _("\
327 --listing-lhs-width set the width in words of the output data column of\n\
328 the listing\n"));
329 fprintf (stream, _("\
330 --listing-lhs-width2 set the width in words of the continuation lines\n\
331 of the output data column; ignored if smaller than\n\
332 the width of the first line\n"));
333 fprintf (stream, _("\
334 --listing-rhs-width set the max width in characters of the lines from\n\
335 the source file\n"));
336 fprintf (stream, _("\
337 --listing-cont-lines set the maximum number of continuation lines used\n\
338 for the output data column of the listing\n"));
339
340 md_show_usage (stream);
341
342 fputc ('\n', stream);
343 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
344}
345
346/* Since it is easy to do here we interpret the special arg "-"
347 to mean "use stdin" and we set that argv[] pointing to "".
348 After we have munged argv[], the only things left are source file
349 name(s) and ""(s) denoting stdin. These file names are used
350 (perhaps more than once) later.
351
352 check for new machine-dep cmdline options in
353 md_parse_option definitions in config/tc-*.c. */
354
355static void
356parse_args (pargc, pargv)
357 int *pargc;
358 char ***pargv;
359{
360 int old_argc, new_argc;
361 char **old_argv, **new_argv;
362
363 /* Starting the short option string with '-' is for programs that
364 expect options and other ARGV-elements in any order and that care about
365 the ordering of the two. We describe each non-option ARGV-element
366 as if it were the argument of an option with character code 1. */
367
368 char *shortopts;
369 extern const char *md_shortopts;
370 static const char std_shortopts[] = {
371 '-', 'J',
372#ifndef WORKING_DOT_WORD
373 /* -K is not meaningful if .word is not being hacked. */
374 'K',
375#endif
376 'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
377#ifndef VMS
378 /* -v takes an argument on VMS, so we don't make it a generic
379 option. */
380 'v',
381#endif
382 'w', 'X',
383 /* New option for extending instruction set (see also --itbl below) */
384 't', ':',
385 '\0'
386 };
387 struct option *longopts;
388 extern struct option md_longopts[];
389 extern size_t md_longopts_size;
390 static const struct option std_longopts[] = {
391#define OPTION_HELP (OPTION_STD_BASE)
392 {"help", no_argument, NULL, OPTION_HELP},
393 /* getopt allows abbreviations, so we do this to stop it from
394 treating -k as an abbreviation for --keep-locals. Some
395 ports use -k to enable PIC assembly. */
396 {"keep-locals", no_argument, NULL, 'L'},
397 {"keep-locals", no_argument, NULL, 'L'},
398 {"mri", no_argument, NULL, 'M'},
399#define OPTION_NOCPP (OPTION_STD_BASE + 1)
400 {"nocpp", no_argument, NULL, OPTION_NOCPP},
401#define OPTION_STATISTICS (OPTION_STD_BASE + 2)
402 {"statistics", no_argument, NULL, OPTION_STATISTICS},
403#define OPTION_VERSION (OPTION_STD_BASE + 3)
404 {"version", no_argument, NULL, OPTION_VERSION},
405#define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
406 {"dump-config", no_argument, NULL, OPTION_DUMPCONFIG},
407#define OPTION_VERBOSE (OPTION_STD_BASE + 5)
408 {"verbose", no_argument, NULL, OPTION_VERBOSE},
409#define OPTION_EMULATION (OPTION_STD_BASE + 6)
410 {"emulation", required_argument, NULL, OPTION_EMULATION},
411#define OPTION_DEFSYM (OPTION_STD_BASE + 7)
412 {"defsym", required_argument, NULL, OPTION_DEFSYM},
413#define OPTION_INSTTBL (OPTION_STD_BASE + 8)
414 /* New option for extending instruction set (see also -t above).
415 The "-t file" or "--itbl file" option extends the basic set of
416 valid instructions by reading "file", a text file containing a
417 list of instruction formats. The additional opcodes and their
418 formats are added to the built-in set of instructions, and
419 mnemonics for new registers may also be defined. */
420 {"itbl", required_argument, NULL, OPTION_INSTTBL},
421#define OPTION_LISTING_LHS_WIDTH (OPTION_STD_BASE + 9)
422 {"listing-lhs-width", required_argument, NULL, OPTION_LISTING_LHS_WIDTH},
423#define OPTION_LISTING_LHS_WIDTH2 (OPTION_STD_BASE + 10)
424 {"listing-lhs-width2", required_argument, NULL, OPTION_LISTING_LHS_WIDTH2},
425#define OPTION_LISTING_RHS_WIDTH (OPTION_STD_BASE + 11)
426 {"listing-rhs-width", required_argument, NULL, OPTION_LISTING_RHS_WIDTH},
427#define OPTION_LISTING_CONT_LINES (OPTION_STD_BASE + 12)
428 {"listing-cont-lines", required_argument, NULL, OPTION_LISTING_CONT_LINES},
429#define OPTION_DEPFILE (OPTION_STD_BASE + 13)
430 {"MD", required_argument, NULL, OPTION_DEPFILE},
431#define OPTION_GSTABS (OPTION_STD_BASE + 14)
432 {"gstabs", no_argument, NULL, OPTION_GSTABS},
433#define OPTION_STRIP_LOCAL_ABSOLUTE (OPTION_STD_BASE + 15)
434 {"strip-local-absolute", no_argument, NULL, OPTION_STRIP_LOCAL_ABSOLUTE},
435#define OPTION_TRADITIONAL_FORMAT (OPTION_STD_BASE + 16)
436 {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
437#define OPTION_GDWARF2 (OPTION_STD_BASE + 17)
438 {"gdwarf2", no_argument, NULL, OPTION_GDWARF2},
439 {"no-warn", no_argument, NULL, 'W'},
440#define OPTION_WARN (OPTION_STD_BASE + 18)
441 {"warn", no_argument, NULL, OPTION_WARN},
442#define OPTION_TARGET_HELP (OPTION_STD_BASE + 19)
443 {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
444#define OPTION_WARN_FATAL (OPTION_STD_BASE + 20)
445 {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL}
446 /* When you add options here, check that they do not collide with
447 OPTION_MD_BASE. See as.h. */
448 };
449
450 /* Construct the option lists from the standard list and the target
451 dependent list. Include space for an extra NULL option and
452 always NULL terminate. */
453 shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
454 longopts = (struct option *) xmalloc (sizeof (std_longopts)
455 + md_longopts_size
456 + sizeof (struct option));
457 memcpy (longopts, std_longopts, sizeof (std_longopts));
458 memcpy ((char *) longopts + sizeof (std_longopts),
459 md_longopts, md_longopts_size);
460 memset ((char *) longopts + sizeof (std_longopts) + md_longopts_size,
461 0, sizeof (struct option));
462
463 /* Make a local copy of the old argv. */
464 old_argc = *pargc;
465 old_argv = *pargv;
466
467 /* Initialize a new argv that contains no options. */
468 new_argv = (char **) xmalloc (sizeof (char *) * (old_argc + 1));
469 new_argv[0] = old_argv[0];
470 new_argc = 1;
471 new_argv[new_argc] = NULL;
472
473 while (1)
474 {
475 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
476 indicate a long option. */
477 int longind;
478 int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
479 &longind);
480
481 if (optc == -1)
482 break;
483
484 switch (optc)
485 {
486 default:
487 /* md_parse_option should return 1 if it recognizes optc,
488 0 if not. */
489 if (md_parse_option (optc, optarg) != 0)
490 break;
491 /* `-v' isn't included in the general short_opts list, so check for
492 it explicity here before deciding we've gotten a bad argument. */
493 if (optc == 'v')
494 {
495#ifdef VMS
496 /* Telling getopt to treat -v's value as optional can result
497 in it picking up a following filename argument here. The
498 VMS code in md_parse_option can return 0 in that case,
499 but it has no way of pushing the filename argument back. */
500 if (optarg && *optarg)
501 new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
502 else
503#else
504 case 'v':
505#endif
506 case OPTION_VERBOSE:
507 print_version_id ();
508 break;
509 }
510 /* Fall through. */
511
512 case '?':
513 exit (EXIT_FAILURE);
514
515 case 1: /* File name. */
516 if (!strcmp (optarg, "-"))
517 optarg = "";
518 new_argv[new_argc++] = optarg;
519 new_argv[new_argc] = NULL;
520 break;
521
522 case OPTION_TARGET_HELP:
523 md_show_usage (stdout);
524 exit (EXIT_SUCCESS);
525
526 case OPTION_HELP:
527 show_usage (stdout);
528 exit (EXIT_SUCCESS);
529
530 case OPTION_NOCPP:
531 break;
532
533 case OPTION_STATISTICS:
534 flag_print_statistics = 1;
535 break;
536
537 case OPTION_STRIP_LOCAL_ABSOLUTE:
538 flag_strip_local_absolute = 1;
539 break;
540
541 case OPTION_TRADITIONAL_FORMAT:
542 flag_traditional_format = 1;
543 break;
544
545 case OPTION_VERSION:
546 /* This output is intended to follow the GNU standards document. */
547#ifdef BFD_ASSEMBLER
548 printf (_("GNU assembler %s\n"), BFD_VERSION_STRING);
549#else
550 printf (_("GNU assembler %s\n"), VERSION);
551#endif
552 printf (_("Copyright 2002 Free Software Foundation, Inc.\n"));
553 printf (_("\
554This program is free software; you may redistribute it under the terms of\n\
555the GNU General Public License. This program has absolutely no warranty.\n"));
556 printf (_("This assembler was configured for a target of `%s'.\n"),
557 TARGET_ALIAS);
558 exit (EXIT_SUCCESS);
559
560 case OPTION_EMULATION:
561#ifdef USE_EMULATIONS
562 if (strcmp (optarg, this_emulation->name))
563 as_fatal (_("multiple emulation names specified"));
564#else
565 as_fatal (_("emulations not handled in this configuration"));
566#endif
567 break;
568
569 case OPTION_DUMPCONFIG:
570 fprintf (stderr, _("alias = %s\n"), TARGET_ALIAS);
571 fprintf (stderr, _("canonical = %s\n"), TARGET_CANONICAL);
572 fprintf (stderr, _("cpu-type = %s\n"), TARGET_CPU);
573#ifdef TARGET_OBJ_FORMAT
574 fprintf (stderr, _("format = %s\n"), TARGET_OBJ_FORMAT);
575#endif
576#ifdef TARGET_FORMAT
577 fprintf (stderr, _("bfd-target = %s\n"), TARGET_FORMAT);
578#endif
579 exit (EXIT_SUCCESS);
580
581 case OPTION_DEFSYM:
582 {
583 char *s;
584 valueT i;
585 struct defsym_list *n;
586
587 for (s = optarg; *s != '\0' && *s != '='; s++)
588 ;
589 if (*s == '\0')
590 as_fatal (_("bad defsym; format is --defsym name=value"));
591 *s++ = '\0';
592#ifdef BFD_ASSEMBLER
593 i = bfd_scan_vma (s, (const char **) NULL, 0);
594#else
595 i = strtol (s, (char **) NULL, 0);
596#endif
597 n = (struct defsym_list *) xmalloc (sizeof *n);
598 n->next = defsyms;
599 n->name = optarg;
600 n->value = i;
601 defsyms = n;
602 }
603 break;
604
605 case OPTION_INSTTBL:
606 case 't':
607 {
608 /* optarg is the name of the file containing the instruction
609 formats, opcodes, register names, etc. */
610 struct itbl_file_list *n;
611
612 if (optarg == NULL)
613 {
614 as_warn (_("no file name following -t option"));
615 break;
616 }
617
618 n = (struct itbl_file_list *) xmalloc (sizeof *n);
619 n->next = itbl_files;
620 n->name = optarg;
621 itbl_files = n;
622
623 /* Parse the file and add the new instructions to our internal
624 table. If multiple instruction tables are specified, the
625 information from this table gets appended onto the existing
626 internal table. */
627 itbl_files->name = xstrdup (optarg);
628 if (itbl_parse (itbl_files->name) != 0)
629 as_fatal (_("failed to read instruction table %s\n"),
630 itbl_files->name);
631 }
632 break;
633
634 case OPTION_DEPFILE:
635 start_dependencies (optarg);
636 break;
637
638 case OPTION_GSTABS:
639 debug_type = DEBUG_STABS;
640 break;
641
642 case OPTION_GDWARF2:
643 debug_type = DEBUG_DWARF2;
644 break;
645
646 case 'J':
647 flag_signed_overflow_ok = 1;
648 break;
649
650#ifndef WORKING_DOT_WORD
651 case 'K':
652 flag_warn_displacement = 1;
653 break;
654#endif
655
656 case 'L':
657 flag_keep_locals = 1;
658 break;
659
660 case OPTION_LISTING_LHS_WIDTH:
661 listing_lhs_width = atoi (optarg);
662 if (listing_lhs_width_second < listing_lhs_width)
663 listing_lhs_width_second = listing_lhs_width;
664 break;
665 case OPTION_LISTING_LHS_WIDTH2:
666 {
667 int tmp = atoi (optarg);
668 if (tmp > listing_lhs_width)
669 listing_lhs_width_second = tmp;
670 }
671 break;
672 case OPTION_LISTING_RHS_WIDTH:
673 listing_rhs_width = atoi (optarg);
674 break;
675 case OPTION_LISTING_CONT_LINES:
676 listing_lhs_cont_lines = atoi (optarg);
677 break;
678
679 case 'M':
680 flag_mri = 1;
681#ifdef TC_M68K
682 flag_m68k_mri = 1;
683#endif
684 break;
685
686 case 'R':
687 flag_readonly_data_in_text = 1;
688 break;
689
690 case 'W':
691 flag_no_warnings = 1;
692 break;
693
694 case OPTION_WARN:
695 flag_no_warnings = 0;
696 flag_fatal_warnings = 0;
697 break;
698
699 case OPTION_WARN_FATAL:
700 flag_no_warnings = 0;
701 flag_fatal_warnings = 1;
702 break;
703
704 case 'Z':
705 flag_always_generate_output = 1;
706 break;
707
708 case 'a':
709 if (optarg)
710 {
711 if (md_parse_option (optc, optarg) != 0)
712 break;
713
714 while (*optarg)
715 {
716 switch (*optarg)
717 {
718 case 'c':
719 listing |= LISTING_NOCOND;
720 break;
721 case 'd':
722 listing |= LISTING_NODEBUG;
723 break;
724 case 'h':
725 listing |= LISTING_HLL;
726 break;
727 case 'l':
728 listing |= LISTING_LISTING;
729 break;
730 case 'm':
731 listing |= LISTING_MACEXP;
732 break;
733 case 'n':
734 listing |= LISTING_NOFORM;
735 break;
736 case 's':
737 listing |= LISTING_SYMBOLS;
738 break;
739 case '=':
740 listing_filename = xstrdup (optarg + 1);
741 optarg += strlen (listing_filename);
742 break;
743 default:
744 as_fatal (_("invalid listing option `%c'"), *optarg);
745 break;
746 }
747 optarg++;
748 }
749 }
750 if (!listing)
751 listing = LISTING_DEFAULT;
752 break;
753
754 case 'D':
755 /* DEBUG is implemented: it debugs different
756 things from other people's assemblers. */
757 flag_debug = 1;
758 break;
759
760 case 'f':
761 flag_no_comments = 1;
762 break;
763
764 case 'I':
765 { /* Include file directory. */
766 char *temp = xstrdup (optarg);
767 add_include_dir (temp);
768 break;
769 }
770
771 case 'o':
772 out_file_name = xstrdup (optarg);
773 break;
774
775 case 'w':
776 break;
777
778 case 'X':
779 /* -X means treat warnings as errors. */
780 break;
781 }
782 }
783
784 free (shortopts);
785 free (longopts);
786
787 *pargc = new_argc;
788 *pargv = new_argv;
789
790#ifdef md_after_parse_args
791 md_after_parse_args ();
792#endif
793}
794
795static long start_time;
796
797int main PARAMS ((int, char **));
798
799int
800main (argc, argv)
801 int argc;
802 char **argv;
803{
804 int macro_alternate;
805 int macro_strip_at;
806 int keep_it;
807
808#ifdef __EMX__
809 _emxload_env ("GCCLOAD");
810 _envargs (&argc, &argv, "GASOPT");
811 _response (&argc, &argv);
812 _wildcard (&argc, &argv);
813#endif /* __EMX__ */
814
815 start_time = get_run_time ();
816
817#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
818 setlocale (LC_MESSAGES, "");
819#endif
820#if defined (HAVE_SETLOCALE)
821 setlocale (LC_CTYPE, "");
822#endif
823 bindtextdomain (PACKAGE, LOCALEDIR);
824 textdomain (PACKAGE);
825
826 if (debug_memory)
827 chunksize = 64;
828
829#ifdef HOST_SPECIAL_INIT
830 HOST_SPECIAL_INIT (argc, argv);
831#endif
832
833#ifdef __EMX__
834 myname = _getname(argv[0]);
835#else
836 myname = argv[0];
837#endif
838 xmalloc_set_program_name (myname);
839
840 START_PROGRESS (myname, 0);
841
842#ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
843#define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
844#endif
845
846 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
847
848 hex_init ();
849#ifdef BFD_ASSEMBLER
850 bfd_init ();
851 bfd_set_error_program_name (myname);
852#endif
853
854#ifdef USE_EMULATIONS
855 select_emulation_mode (argc, argv);
856#endif
857
858 PROGRESS (1);
859 symbol_begin ();
860 frag_init ();
861 subsegs_begin ();
862 parse_args (&argc, &argv);
863
864#ifdef EMX
865 if (emx_omf)
866 {
867 char *tmp_dir;
868 size_t tmp_dir_len;
869
870 omf_file_name = out_file_name;
871 tmp_dir = getenv ("TMPDIR");
872 if (tmp_dir == NULL) tmp_dir = getenv ("TMP");
873 if (tmp_dir == NULL) tmp_dir = getenv ("TEMP");
874 if (tmp_dir == NULL) tmp_dir = ".";
875 tmp_dir_len = strlen (tmp_dir);
876 out_file_name = xmalloc (tmp_dir_len + 10);
877 memcpy (out_file_name, tmp_dir, tmp_dir_len);
878 if (tmp_dir_len != 0 && strchr ("\\/:", tmp_dir[tmp_dir_len-1]) == NULL)
879 out_file_name[tmp_dir_len++] = '\\';
880 strcpy (out_file_name + tmp_dir_len, "asXXXXXX");
881 if (mktemp (out_file_name) == NULL)
882 as_fatal ("mktemp() failed");
883 }
884#endif /* EMX */
885
886 read_begin ();
887 input_scrub_begin ();
888 expr_begin ();
889
890 if (flag_print_statistics)
891 xatexit (dump_statistics);
892
893 macro_alternate = 0;
894 macro_strip_at = 0;
895#ifdef TC_I960
896 macro_strip_at = flag_mri;
897#endif
898#ifdef TC_A29K
899 /* For compatibility with the AMD 29K family macro assembler
900 specification. */
901 macro_alternate = 1;
902 macro_strip_at = 1;
903#endif
904
905 macro_init (macro_alternate, flag_mri, macro_strip_at, macro_expr);
906
907 PROGRESS (1);
908
909#ifdef BFD_ASSEMBLER
910 output_file_create (out_file_name);
911 assert (stdoutput != 0);
912#endif
913
914#ifdef tc_init_after_args
915 tc_init_after_args ();
916#endif
917
918 itbl_init ();
919
920 /* Now that we have fully initialized, and have created the output
921 file, define any symbols requested by --defsym command line
922 arguments. */
923 while (defsyms != NULL)
924 {
925 symbolS *sym;
926 struct defsym_list *next;
927
928 sym = symbol_new (defsyms->name, absolute_section, defsyms->value,
929 &zero_address_frag);
930 symbol_table_insert (sym);
931 next = defsyms->next;
932 free (defsyms);
933 defsyms = next;
934 }
935
936 PROGRESS (1);
937
938 /* Assemble it. */
939 perform_an_assembly_pass (argc, argv);
940
941 cond_finish_check (-1);
942
943#ifdef md_end
944 md_end ();
945#endif
946
947 /* If we've been collecting dwarf2 .debug_line info, either for
948 assembly debugging or on behalf of the compiler, emit it now. */
949 dwarf2_finish ();
950
951 if (seen_at_least_1_file ()
952 && (flag_always_generate_output || had_errors () == 0))
953 keep_it = 1;
954 else
955 keep_it = 0;
956
957#if defined (BFD_ASSEMBLER) || !defined (BFD)
958 /* This used to be done at the start of write_object_file in
959 write.c, but that caused problems when doing listings when
960 keep_it was zero. This could probably be moved above md_end, but
961 I didn't want to risk the change. */
962 subsegs_finish ();
963#endif
964
965 if (keep_it)
966 write_object_file ();
967
968#ifndef NO_LISTING
969 listing_print (listing_filename);
970#endif
971
972#ifndef OBJ_VMS /* does its own file handling */
973#ifndef BFD_ASSEMBLER
974 if (keep_it)
975#endif
976 output_file_close (out_file_name);
977#endif
978
979 if (flag_fatal_warnings && had_warnings () > 0 && had_errors () == 0)
980 as_bad (_("%d warnings, treating warnings as errors"), had_warnings ());
981
982 if (had_errors () > 0 && ! flag_always_generate_output)
983 keep_it = 0;
984
985 if (!keep_it)
986 unlink (out_file_name);
987
988#ifdef EMX
989 if (keep_it && emx_omf)
990 {
991 int rc, i;
992 char *args[6];
993
994 i = 0;
995 args[i++] = "emxomf";
996 if (emx_strip)
997 args[i++] = "-s";
998 args[i++] = "-o";
999 args[i++] = omf_file_name;
1000 args[i++] = out_file_name;
1001 args[i] = NULL;
1002 rc = spawnvp (P_WAIT, "emxomf.exe", args);
1003 remove (out_file_name);
1004 if (rc < 0)
1005 as_fatal ("cannot run emxomf");
1006 else if (rc > 0)
1007 as_fatal ("emxomf failed");
1008 }
1009#endif /* EMX */
1010
1011 input_scrub_end ();
1012
1013 END_PROGRESS (myname);
1014
1015 /* Use xexit instead of return, because under VMS environments they
1016 may not place the same interpretation on the value given. */
1017 if (had_errors () > 0)
1018 xexit (EXIT_FAILURE);
1019
1020 /* Only generate dependency file if assembler was successful. */
1021 print_dependencies ();
1022
1023 xexit (EXIT_SUCCESS);
1024}
1025
1026static void
1027dump_statistics ()
1028{
1029#ifdef HAVE_SBRK
1030 char *lim = (char *) sbrk (0);
1031#endif
1032 long run_time = get_run_time () - start_time;
1033
1034 fprintf (stderr, _("%s: total time in assembly: %ld.%06ld\n"),
1035 myname, run_time / 1000000, run_time % 1000000);
1036#ifdef HAVE_SBRK
1037 fprintf (stderr, _("%s: data size %ld\n"),
1038 myname, (long) (lim - (char *) &environ));
1039#endif
1040
1041 subsegs_print_statistics (stderr);
1042 write_print_statistics (stderr);
1043 symbol_print_statistics (stderr);
1044 read_print_statistics (stderr);
1045
1046#ifdef tc_print_statistics
1047 tc_print_statistics (stderr);
1048#endif
1049#ifdef obj_print_statistics
1050 obj_print_statistics (stderr);
1051#endif
1052}
1053
1054
1055/* Here to attempt 1 pass over each input file.
1056 We scan argv[*] looking for filenames or exactly "" which is
1057 shorthand for stdin. Any argv that is NULL is not a file-name.
1058 We set need_pass_2 TRUE if, after this, we still have unresolved
1059 expressions of the form (unknown value)+-(unknown value).
1060
1061 Note the un*x semantics: there is only 1 logical input file, but it
1062 may be a catenation of many 'physical' input files. */
1063
1064static void
1065perform_an_assembly_pass (argc, argv)
1066 int argc;
1067 char **argv;
1068{
1069 int saw_a_file = 0;
1070#ifdef BFD_ASSEMBLER
1071 flagword applicable;
1072#endif
1073
1074 need_pass_2 = 0;
1075
1076#ifndef BFD_ASSEMBLER
1077#ifdef MANY_SEGMENTS
1078 {
1079 unsigned int i;
1080 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1081 segment_info[i].fix_root = 0;
1082 }
1083 /* Create the three fixed ones. */
1084 {
1085 segT seg;
1086
1087#ifdef TE_APOLLO
1088 seg = subseg_new (".wtext", 0);
1089#else
1090 seg = subseg_new (".text", 0);
1091#endif
1092 assert (seg == SEG_E0);
1093 seg = subseg_new (".data", 0);
1094 assert (seg == SEG_E1);
1095 seg = subseg_new (".bss", 0);
1096 assert (seg == SEG_E2);
1097#ifdef TE_APOLLO
1098 create_target_segments ();
1099#endif
1100 }
1101
1102#else /* not MANY_SEGMENTS */
1103 text_fix_root = NULL;
1104 data_fix_root = NULL;
1105 bss_fix_root = NULL;
1106#endif /* not MANY_SEGMENTS */
1107#else /* BFD_ASSEMBLER */
1108 /* Create the standard sections, and those the assembler uses
1109 internally. */
1110 text_section = subseg_new (TEXT_SECTION_NAME, 0);
1111 data_section = subseg_new (DATA_SECTION_NAME, 0);
1112 bss_section = subseg_new (BSS_SECTION_NAME, 0);
1113 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
1114 to have relocs, otherwise we don't find out in time. */
1115 applicable = bfd_applicable_section_flags (stdoutput);
1116 bfd_set_section_flags (stdoutput, text_section,
1117 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1118 | SEC_CODE | SEC_READONLY));
1119 bfd_set_section_flags (stdoutput, data_section,
1120 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1121 | SEC_DATA));
1122 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
1123 seg_info (bss_section)->bss = 1;
1124 subseg_new (BFD_ABS_SECTION_NAME, 0);
1125 subseg_new (BFD_UND_SECTION_NAME, 0);
1126 reg_section = subseg_new ("*GAS `reg' section*", 0);
1127 expr_section = subseg_new ("*GAS `expr' section*", 0);
1128
1129#endif /* BFD_ASSEMBLER */
1130
1131 subseg_set (text_section, 0);
1132
1133 /* This may add symbol table entries, which requires having an open BFD,
1134 and sections already created, in BFD_ASSEMBLER mode. */
1135 md_begin ();
1136
1137#ifdef USING_CGEN
1138 gas_cgen_begin ();
1139#endif
1140#ifdef obj_begin
1141 obj_begin ();
1142#endif
1143
1144 /* Skip argv[0]. */
1145 argv++;
1146 argc--;
1147
1148 while (argc--)
1149 {
1150 if (*argv)
1151 { /* Is it a file-name argument? */
1152 PROGRESS (1);
1153 saw_a_file++;
1154 /* argv->"" if stdin desired, else->filename */
1155 read_a_source_file (*argv);
1156 }
1157 argv++; /* completed that argv */
1158 }
1159 if (!saw_a_file)
1160 read_a_source_file ("");
1161}
1162
1163/* The interface between the macro code and gas expression handling. */
1164
1165static int
1166macro_expr (emsg, idx, in, val)
1167 const char *emsg;
1168 int idx;
1169 sb *in;
1170 int *val;
1171{
1172 char *hold;
1173 expressionS ex;
1174
1175 sb_terminate (in);
1176
1177 hold = input_line_pointer;
1178 input_line_pointer = in->ptr + idx;
1179 expression (&ex);
1180 idx = input_line_pointer - in->ptr;
1181 input_line_pointer = hold;
1182
1183 if (ex.X_op != O_constant)
1184 as_bad ("%s", emsg);
1185
1186 *val = (int) ex.X_add_number;
1187
1188 return idx;
1189}
Note: See TracBrowser for help on using the repository browser.