1 /* as.c - GAS main program.
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 1996
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 * Main program for AS; a 32-bit assembler of GNU.
23 * Understands command arguments.
24 * Has a few routines that don't fit in other modules because they
31 * Since no-one else says they will support them in future: I
32 * don't support them now.
37 #include "libiberty.h"
43 #include "output-file.h"
47 static void perform_an_assembly_pass
PARAMS ((int argc
, char **argv
));
48 static int macro_expr
PARAMS ((const char *, int, sb
*, int *));
50 int listing
; /* true if a listing is wanted */
52 static char *listing_filename
= NULL
; /* Name of listing file. */
54 /* Maximum level of macro nesting. */
56 int max_macro_nest
= 100;
58 char *myname
; /* argv[0] */
60 segT reg_section
, expr_section
;
61 segT text_section
, data_section
, bss_section
;
66 /* To monitor memory allocation more effectively, make this non-zero.
67 Then the chunk sizes for gas and bfd will be reduced. */
79 fprintf (stderr
, "GNU assembler version %s (%s)", GAS_VERSION
, TARGET_ALIAS
);
81 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
83 fprintf (stderr
, "\n");
90 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
94 -a[sub-option...] turn on listings\n\
95 Sub-options [default hls]:\n\
96 d omit debugging directives\n\
97 h include high-level source\n\
99 n omit forms processing\n\
101 =file set listing file name (must be last sub-option)\n");
103 -D produce assembler debugging messages\n\
104 --defsym SYM=VAL define symbol SYM to given value\n\
105 -f skip whitespace and comment preprocessing\n\
106 --help show this message and exit\n\
107 -I DIR add DIR to search list for .include directives\n\
108 -J don't warn about signed overflow\n\
109 -K warn when differences altered for long displacements\n\
110 -L keep local symbols (starting with `L')\n");
112 -M,--mri assemble in MRI compatibility mode\n\
114 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
115 -R fold data section into text section\n\
116 --statistics print various measured statistics from execution\n\
117 --version print assembler version number and exit\n\
118 -W suppress warnings\n\
121 -Z generate object file even after errors\n");
123 md_show_usage (stream
);
126 #ifdef USE_EMULATIONS
127 #define EMULATION_ENVIRON "AS_EMULATION"
129 extern struct emulation mipsbelf
, mipslelf
, mipself
;
130 extern struct emulation mipsbecoff
, mipslecoff
, mipsecoff
;
132 static struct emulation
*const emulations
[] = { EMULATIONS
};
133 static const int n_emulations
= sizeof (emulations
) / sizeof (emulations
[0]);
136 select_emulation_mode (argc
, argv
)
143 for (i
= 1; i
< argc
; i
++)
144 if (!strncmp ("--em", argv
[i
], 4))
150 p
= strchr (argv
[i
], '=');
157 as_fatal ("missing emulation mode name");
162 em
= getenv (EMULATION_ENVIRON
);
164 em
= DEFAULT_EMULATION
;
168 for (i
= 0; i
< n_emulations
; i
++)
169 if (!strcmp (emulations
[i
]->name
, em
))
171 if (i
== n_emulations
)
172 as_fatal ("unrecognized emulation name `%s'", em
);
173 this_emulation
= emulations
[i
];
176 this_emulation
= emulations
[0];
178 this_emulation
->init ();
182 default_emul_bfd_name ()
191 this_format
= this_emulation
->format
;
193 if (this_emulation
->leading_underscore
== 2)
194 this_emulation
->leading_underscore
= this_format
->dfl_leading_underscore
;
196 if (this_emulation
->default_endian
!= 2)
197 target_big_endian
= this_emulation
->default_endian
;
199 if (this_emulation
->fake_label_name
== 0)
201 if (this_emulation
->leading_underscore
)
202 this_emulation
->fake_label_name
= "L0\001";
204 /* What other parameters should we test? */
205 this_emulation
->fake_label_name
= ".L0\001";
211 * Since it is easy to do here we interpret the special arg "-"
212 * to mean "use stdin" and we set that argv[] pointing to "".
213 * After we have munged argv[], the only things left are source file
214 * name(s) and ""(s) denoting stdin. These file names are used
215 * (perhaps more than once) later.
217 * check for new machine-dep cmdline options in
218 * md_parse_option definitions in config/tc-*.c
222 parse_args (pargc
, pargv
)
226 int old_argc
, new_argc
;
227 char **old_argv
, **new_argv
;
229 /* Starting the short option string with '-' is for programs that
230 expect options and other ARGV-elements in any order and that care about
231 the ordering of the two. We describe each non-option ARGV-element
232 as if it were the argument of an option with character code 1. */
235 extern CONST
char *md_shortopts
;
236 static const char std_shortopts
[] =
239 #ifndef WORKING_DOT_WORD
240 /* -K is not meaningful if .word is not being hacked. */
243 'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
245 /* -v takes an argument on VMS, so we don't make it a generic
252 struct option
*longopts
;
253 extern struct option md_longopts
[];
254 extern size_t md_longopts_size
;
255 static const struct option std_longopts
[] = {
256 #define OPTION_HELP (OPTION_STD_BASE)
257 {"help", no_argument
, NULL
, OPTION_HELP
},
258 {"mri", no_argument
, NULL
, 'M'},
259 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
260 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
261 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
262 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
263 #define OPTION_VERSION (OPTION_STD_BASE + 3)
264 {"version", no_argument
, NULL
, OPTION_VERSION
},
265 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
266 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
267 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
268 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
269 #define OPTION_EMULATION (OPTION_STD_BASE + 6)
270 {"emulation", required_argument
, NULL
, OPTION_EMULATION
},
271 #define OPTION_DEFSYM (OPTION_STD_BASE + 7)
272 {"defsym", required_argument
, NULL
, OPTION_DEFSYM
}
275 /* Construct the option lists from the standard list and the
276 target dependent list. */
277 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
278 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
279 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
280 memcpy ((char *) longopts
+ sizeof (std_longopts
),
281 md_longopts
, md_longopts_size
);
283 /* Make a local copy of the old argv. */
287 /* Initialize a new argv that contains no options. */
288 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
289 new_argv
[0] = old_argv
[0];
291 new_argv
[new_argc
] = NULL
;
295 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
296 indicate a long option. */
298 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
307 /* md_parse_option should return 1 if it recognizes optc,
309 if (md_parse_option (optc
, optarg
) != 0)
311 /* `-v' isn't included in the general short_opts list, so check for
312 it explicity here before deciding we've gotten a bad argument. */
316 /* Telling getopt to treat -v's value as optional can result
317 in it picking up a following filename argument here. The
318 VMS code in md_parse_option can return 0 in that case,
319 but it has no way of pushing the filename argument back. */
320 if (optarg
&& *optarg
)
321 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
335 case 1: /* File name. */
336 if (!strcmp (optarg
, "-"))
338 new_argv
[new_argc
++] = optarg
;
339 new_argv
[new_argc
] = NULL
;
349 case OPTION_STATISTICS
:
350 flag_print_statistics
= 1;
357 case OPTION_EMULATION
:
358 #ifdef USE_EMULATIONS
359 if (strcmp (optarg
, this_emulation
->name
))
360 as_fatal ("multiple emulation names specified");
362 as_fatal ("emulations not handled in this configuration");
366 case OPTION_DUMPCONFIG
:
367 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
368 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
369 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
370 #ifdef TARGET_OBJ_FORMAT
371 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
374 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
384 for (s
= optarg
; *s
!= '\0' && *s
!= '='; s
++)
387 as_fatal ("bad defsym; format is --defsym name=value");
389 i
= strtol (s
, (char **) NULL
, 0);
390 sym
= symbol_new (optarg
, absolute_section
, (valueT
) i
,
392 symbol_table_insert (sym
);
397 flag_signed_overflow_ok
= 1;
400 #ifndef WORKING_DOT_WORD
402 flag_warn_displacement
= 1;
407 flag_keep_locals
= 1;
418 flag_readonly_data_in_text
= 1;
422 flag_no_warnings
= 1;
426 flag_always_generate_output
= 1;
437 listing
|= LISTING_NODEBUG
;
440 listing
|= LISTING_HLL
;
443 listing
|= LISTING_LISTING
;
446 listing
|= LISTING_NOFORM
;
449 listing
|= LISTING_SYMBOLS
;
452 listing_filename
= xstrdup (optarg
+ 1);
453 optarg
+= strlen (listing_filename
);
456 as_fatal ("invalid listing option `%c'", *optarg
);
463 listing
= LISTING_DEFAULT
;
467 /* DEBUG is implemented: it debugs different */
468 /* things from other people's assemblers. */
473 flag_no_comments
= 1;
477 { /* Include file directory */
478 char *temp
= xstrdup (optarg
);
479 add_include_dir (temp
);
484 out_file_name
= xstrdup (optarg
);
491 /* -X means treat warnings as errors */
503 static void dump_statistics ();
504 static long start_time
;
515 start_time
= get_run_time ();
520 extern long _bfd_chunksize
;
526 #ifdef HOST_SPECIAL_INIT
527 HOST_SPECIAL_INIT (argc
, argv
);
531 xmalloc_set_program_name (myname
);
533 START_PROGRESS (myname
, 0);
535 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
536 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
539 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
544 bfd_set_error_program_name (myname
);
547 #ifdef USE_EMULATIONS
548 select_emulation_mode (argc
, argv
);
555 parse_args (&argc
, &argv
);
557 input_scrub_begin ();
560 if (flag_print_statistics
)
561 xatexit (dump_statistics
);
566 macro_strip_at
= flag_mri
;
569 /* For compatibility with the AMD 29K family macro assembler
575 macro_init (macro_alternate
, flag_mri
, macro_strip_at
, macro_expr
);
580 output_file_create (out_file_name
);
581 assert (stdoutput
!= 0);
584 #ifdef tc_init_after_args
585 tc_init_after_args ();
590 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
596 if (seen_at_least_1_file ()
597 && !((had_warnings () && flag_always_generate_output
)
598 || had_errors () > 0))
604 write_object_file ();
607 listing_print (listing_filename
);
610 #ifndef OBJ_VMS /* does its own file handling */
611 #ifndef BFD_ASSEMBLER
614 output_file_close (out_file_name
);
618 unlink (out_file_name
);
622 END_PROGRESS (myname
);
624 /* Use xexit instead of return, because under VMS environments they
625 may not place the same interpretation on the value given. */
626 if ((had_warnings () && flag_always_generate_output
)
627 || had_errors () > 0)
628 xexit (EXIT_FAILURE
);
629 xexit (EXIT_SUCCESS
);
635 extern char **environ
;
637 char *lim
= (char *) sbrk (0);
639 long run_time
= get_run_time () - start_time
;
641 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
642 myname
, run_time
/ 1000000, run_time
% 1000000);
644 fprintf (stderr
, "%s: data size %ld\n",
645 myname
, (long) (lim
- (char *) &environ
));
648 subsegs_print_statistics (stderr
);
649 write_print_statistics (stderr
);
650 symbol_print_statistics (stderr
);
651 read_print_statistics (stderr
);
653 #ifdef tc_print_statistics
654 tc_print_statistics (stderr
);
656 #ifdef obj_print_statistics
657 obj_print_statistics (stderr
);
662 /* perform_an_assembly_pass()
664 * Here to attempt 1 pass over each input file.
665 * We scan argv[*] looking for filenames or exactly "" which is
666 * shorthand for stdin. Any argv that is NULL is not a file-name.
667 * We set need_pass_2 TRUE if, after this, we still have unresolved
668 * expressions of the form (unknown value)+-(unknown value).
670 * Note the un*x semantics: there is only 1 logical input file, but it
671 * may be a catenation of many 'physical' input files.
674 perform_an_assembly_pass (argc
, argv
)
685 #ifndef BFD_ASSEMBLER
689 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
690 segment_info
[i
].fix_root
= 0;
692 /* Create the three fixed ones */
697 seg
= subseg_new (".wtext", 0);
699 seg
= subseg_new (".text", 0);
701 assert (seg
== SEG_E0
);
702 seg
= subseg_new (".data", 0);
703 assert (seg
== SEG_E1
);
704 seg
= subseg_new (".bss", 0);
705 assert (seg
== SEG_E2
);
707 create_target_segments ();
711 #else /* not MANY_SEGMENTS */
712 text_fix_root
= NULL
;
713 data_fix_root
= NULL
;
715 #endif /* not MANY_SEGMENTS */
716 #else /* BFD_ASSEMBLER */
717 /* Create the standard sections, and those the assembler uses
719 text_section
= subseg_new (".text", 0);
720 data_section
= subseg_new (".data", 0);
721 bss_section
= subseg_new (".bss", 0);
722 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
723 to have relocs, otherwise we don't find out in time. */
724 applicable
= bfd_applicable_section_flags (stdoutput
);
725 bfd_set_section_flags (stdoutput
, text_section
,
726 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
727 | SEC_CODE
| SEC_READONLY
));
728 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
729 bfd_set_section_flags (stdoutput
, data_section
,
730 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
731 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
732 seg_info (bss_section
)->bss
= 1;
733 subseg_new (BFD_ABS_SECTION_NAME
, 0);
734 subseg_new (BFD_UND_SECTION_NAME
, 0);
735 reg_section
= subseg_new ("*GAS `reg' section*", 0);
736 expr_section
= subseg_new ("*GAS `expr' section*", 0);
738 #endif /* BFD_ASSEMBLER */
740 subseg_set (text_section
, 0);
742 /* This may add symbol table entries, which requires having an open BFD,
743 and sections already created, in BFD_ASSEMBLER mode. */
746 argv
++; /* skip argv[0] */
747 argc
--; /* skip argv[0] */
751 { /* Is it a file-name argument? */
754 /* argv->"" if stdin desired, else->filename */
755 read_a_source_file (*argv
);
757 argv
++; /* completed that argv */
760 read_a_source_file ("");
761 } /* perform_an_assembly_pass() */
763 /* The interface between the macro code and gas expression handling. */
766 macro_expr (emsg
, idx
, in
, val
)
777 hold
= input_line_pointer
;
778 input_line_pointer
= in
->ptr
+ idx
;
780 idx
= input_line_pointer
- in
->ptr
;
781 input_line_pointer
= hold
;
783 if (ex
.X_op
!= O_constant
)
786 *val
= (int) ex
.X_add_number
;