source: vendor/binutils/current/gas/as.c

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

binutils v2.14 - offical sources.

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