1 /* as.c - GAS main program.
2 Copyright (C) 1987, 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 * Main program for AS; a 32-bit assembler of GNU.
22 * Understands command arguments.
23 * Has a few routines that don't fit in other modules because they
30 * Since no-one else says they will support them in future: I
31 * don't support them now.
36 #include "libiberty.h"
42 #include "output-file.h"
46 static void perform_an_assembly_pass
PARAMS ((int argc
, char **argv
));
47 static int macro_expr
PARAMS ((const char *, int, sb
*, int *));
49 int listing
; /* true if a listing is wanted */
51 static char *listing_filename
= NULL
; /* Name of listing file. */
53 /* Maximum level of macro nesting. */
55 int max_macro_nest
= 100;
57 char *myname
; /* argv[0] */
59 segT reg_section
, expr_section
;
60 segT text_section
, data_section
, bss_section
;
72 fprintf (stderr
, "GNU assembler version %s (%s)", GAS_VERSION
, TARGET_ALIAS
);
74 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
76 fprintf (stderr
, "\n");
83 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
87 -a[sub-option...] turn on listings\n\
88 Sub-options [default hls]:\n\
89 d omit debugging directives\n\
90 h include high-level source\n\
92 n omit forms processing\n\
94 =file set listing file name (must be last sub-option)\n");
96 -D produce assembler debugging messages\n\
97 -f skip whitespace and comment preprocessing\n\
98 --help show this message and exit\n\
99 -I DIR add DIR to search list for .include directives\n\
100 -J don't warn about signed overflow\n\
101 -K warn when differences altered for long displacements\n\
102 -L keep local symbols (starting with `L')\n");
104 -M,--mri assemble in MRI compatibility mode\n\
106 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
107 -R fold data section into text section\n\
108 --statistics print maximum bytes and total seconds used\n\
109 --version print assembler version number and exit\n\
110 -W suppress warnings\n\
113 -Z generate object file even after errors\n");
115 md_show_usage (stream
);
118 #ifdef USE_EMULATIONS
119 #define EMULATION_ENVIRON "AS_EMULATION"
121 extern struct emulation mipsbelf
, mipslelf
, mipself
;
122 extern struct emulation mipsbecoff
, mipslecoff
, mipsecoff
;
124 static const char *emulation_name
;
125 static struct emulation
*const emulations
[] = { EMULATIONS
};
126 static const int n_emulations
= sizeof (emulations
) / sizeof (emulations
[0]);
129 select_emulation_mode (argc
, argv
)
136 for (i
= 1; i
< argc
; i
++)
137 if (!strncmp ("--em", argv
[i
], 4))
143 p
= strchr (argv
[i
], '=');
150 as_fatal ("missing emulation mode name");
155 em
= getenv (EMULATION_ENVIRON
);
157 em
= DEFAULT_EMULATION
;
161 for (i
= 0; i
< n_emulations
; i
++)
162 if (!strcmp (emulations
[i
]->name
, em
))
164 if (i
== n_emulations
)
165 as_fatal ("unrecognized emulation name `%s'", em
);
166 this_emulation
= emulations
[i
];
169 this_emulation
= emulations
[0];
171 this_emulation
->init ();
175 default_emul_bfd_name ()
183 this_format
= this_emulation
->format
;
185 if (this_emulation
->leading_underscore
== 2)
186 this_emulation
->leading_underscore
= this_format
->dfl_leading_underscore
;
188 if (this_emulation
->default_endian
!= 2)
189 target_big_endian
= this_emulation
->default_endian
;
191 if (this_emulation
->fake_label_name
== 0)
193 if (this_emulation
->leading_underscore
)
194 this_emulation
->fake_label_name
= "L0\001";
196 /* What other parameters should we test? */
197 this_emulation
->fake_label_name
= ".L0\001";
203 * Since it is easy to do here we interpret the special arg "-"
204 * to mean "use stdin" and we set that argv[] pointing to "".
205 * After we have munged argv[], the only things left are source file
206 * name(s) and ""(s) denoting stdin. These file names are used
207 * (perhaps more than once) later.
209 * check for new machine-dep cmdline options in
210 * md_parse_option definitions in config/tc-*.c
214 parse_args (pargc
, pargv
)
218 int old_argc
, new_argc
;
219 char **old_argv
, **new_argv
;
221 /* Starting the short option string with '-' is for programs that
222 expect options and other ARGV-elements in any order and that care about
223 the ordering of the two. We describe each non-option ARGV-element
224 as if it were the argument of an option with character code 1. */
227 extern CONST
char *md_shortopts
;
228 static const char std_shortopts
[] =
231 #ifndef WORKING_DOT_WORD
232 /* -K is not meaningful if .word is not being hacked. */
235 'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
237 /* -v takes an argument on VMS, so we don't make it a generic
244 struct option
*longopts
;
245 extern struct option md_longopts
[];
246 extern size_t md_longopts_size
;
247 static const struct option std_longopts
[] = {
248 #define OPTION_HELP (OPTION_STD_BASE)
249 {"help", no_argument
, NULL
, OPTION_HELP
},
250 {"mri", no_argument
, NULL
, 'M'},
251 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
252 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
253 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
254 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
255 #define OPTION_VERSION (OPTION_STD_BASE + 3)
256 {"version", no_argument
, NULL
, OPTION_VERSION
},
257 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
258 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
259 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
260 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
261 #define OPTION_EMULATION (OPTION_STD_BASE + 6)
262 {"emulation", required_argument
, NULL
, OPTION_EMULATION
},
265 /* Construct the option lists from the standard list and the
266 target dependent list. */
267 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
268 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
269 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
270 memcpy ((char *) longopts
+ sizeof (std_longopts
),
271 md_longopts
, md_longopts_size
);
273 /* Make a local copy of the old argv. */
277 /* Initialize a new argv that contains no options. */
278 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
279 new_argv
[0] = old_argv
[0];
281 new_argv
[new_argc
] = NULL
;
285 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
286 indicate a long option. */
288 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
297 /* md_parse_option should return 1 if it recognizes optc,
299 if (md_parse_option (optc
, optarg
) != 0)
301 /* `-v' isn't included in the general short_opts list, so check for
302 it explicity here before deciding we've gotten a bad argument. */
306 /* Telling getopt to treat -v's value as optional can result
307 in it picking up a following filename argument here. The
308 VMS code in md_parse_option can return 0 in that case,
309 but it has no way of pushing the filename argument back. */
310 if (optarg
&& *optarg
)
311 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
325 case 1: /* File name. */
326 if (!strcmp (optarg
, "-"))
328 new_argv
[new_argc
++] = optarg
;
329 new_argv
[new_argc
] = NULL
;
339 case OPTION_STATISTICS
:
340 flag_print_statistics
= 1;
347 case OPTION_EMULATION
:
348 #ifdef USE_EMULATIONS
349 if (strcmp (optarg
, this_emulation
->name
))
350 as_fatal ("multiple emulation names specified");
352 as_fatal ("emulations not handled in this configuration");
356 case OPTION_DUMPCONFIG
:
357 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
358 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
359 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
360 #ifdef TARGET_OBJ_FORMAT
361 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
364 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
369 flag_signed_overflow_ok
= 1;
372 #ifndef WORKING_DOT_WORD
374 flag_warn_displacement
= 1;
379 flag_keep_locals
= 1;
387 flag_readonly_data_in_text
= 1;
391 flag_no_warnings
= 1;
395 flag_always_generate_output
= 1;
406 listing
|= LISTING_NODEBUG
;
409 listing
|= LISTING_HLL
;
412 listing
|= LISTING_LISTING
;
415 listing
|= LISTING_NOFORM
;
418 listing
|= LISTING_SYMBOLS
;
421 listing_filename
= strdup (optarg
+ 1);
422 if (listing_filename
== NULL
)
423 as_fatal ("virtual memory exhausted");
424 optarg
+= strlen (listing_filename
);
427 as_fatal ("invalid listing option `%c'", *optarg
);
434 listing
= LISTING_DEFAULT
;
438 /* DEBUG is implemented: it debugs different */
439 /* things from other people's assemblers. */
444 flag_no_comments
= 1;
448 { /* Include file directory */
449 char *temp
= strdup (optarg
);
451 as_fatal ("virtual memory exhausted");
452 add_include_dir (temp
);
457 out_file_name
= strdup (optarg
);
459 as_fatal ("virtual memory exhausted");
466 /* -X means treat warnings as errors */
484 long start_time
= get_run_time ();
486 #ifdef HOST_SPECIAL_INIT
487 HOST_SPECIAL_INIT (argc
, argv
);
491 xmalloc_set_program_name (myname
);
493 START_PROGRESS (myname
, 0);
495 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
496 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
499 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
506 #ifdef USE_EMULATIONS
507 select_emulation_mode (argc
, argv
);
515 parse_args (&argc
, &argv
);
516 input_scrub_begin ();
518 macro_init (0, flag_mri
, macro_expr
);
523 output_file_create (out_file_name
);
524 assert (stdoutput
!= 0);
527 #ifdef tc_init_after_args
528 tc_init_after_args ();
533 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
537 /* start-sanitize-rce */
541 /* end-sanitize-rce */
543 if (seen_at_least_1_file ()
544 && !((had_warnings () && flag_always_generate_output
)
545 || had_errors () > 0))
551 write_object_file ();
554 listing_print (listing_filename
);
557 #ifndef OBJ_VMS /* does its own file handling */
558 #ifndef BFD_ASSEMBLER
561 output_file_close (out_file_name
);
565 unlink (out_file_name
);
572 END_PROGRESS (myname
);
574 if (flag_print_statistics
)
576 extern char **environ
;
578 char *lim
= (char *) sbrk (0);
580 long run_time
= get_run_time () - start_time
;
582 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
583 myname
, run_time
/ 1000000, run_time
% 1000000);
585 fprintf (stderr
, "%s: data size %ld\n",
586 myname
, (long) (lim
- (char *) &environ
));
590 /* Use exit instead of return, because under VMS environments they
591 may not place the same interpretation on the value given. */
592 if ((had_warnings () && flag_always_generate_output
)
593 || had_errors () > 0)
599 /* perform_an_assembly_pass()
601 * Here to attempt 1 pass over each input file.
602 * We scan argv[*] looking for filenames or exactly "" which is
603 * shorthand for stdin. Any argv that is NULL is not a file-name.
604 * We set need_pass_2 TRUE if, after this, we still have unresolved
605 * expressions of the form (unknown value)+-(unknown value).
607 * Note the un*x semantics: there is only 1 logical input file, but it
608 * may be a catenation of many 'physical' input files.
611 perform_an_assembly_pass (argc
, argv
)
622 #ifndef BFD_ASSEMBLER
626 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
627 segment_info
[i
].fix_root
= 0;
629 /* Create the three fixed ones */
634 seg
= subseg_new (".wtext", 0);
636 seg
= subseg_new (".text", 0);
638 assert (seg
== SEG_E0
);
639 seg
= subseg_new (".data", 0);
640 assert (seg
== SEG_E1
);
641 seg
= subseg_new (".bss", 0);
642 assert (seg
== SEG_E2
);
644 create_target_segments ();
648 #else /* not MANY_SEGMENTS */
649 text_fix_root
= NULL
;
650 data_fix_root
= NULL
;
652 #endif /* not MANY_SEGMENTS */
653 #else /* BFD_ASSEMBLER */
654 /* Create the standard sections, and those the assembler uses
656 text_section
= subseg_new (".text", 0);
657 data_section
= subseg_new (".data", 0);
658 bss_section
= subseg_new (".bss", 0);
659 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
660 to have relocs, otherwise we don't find out in time. */
661 applicable
= bfd_applicable_section_flags (stdoutput
);
662 bfd_set_section_flags (stdoutput
, text_section
,
663 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
664 | SEC_CODE
| SEC_READONLY
));
665 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
666 bfd_set_section_flags (stdoutput
, data_section
,
667 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
668 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
669 seg_info (bss_section
)->bss
= 1;
670 subseg_new (BFD_ABS_SECTION_NAME
, 0);
671 subseg_new (BFD_UND_SECTION_NAME
, 0);
672 reg_section
= subseg_new ("*GAS `reg' section*", 0);
673 expr_section
= subseg_new ("*GAS `expr' section*", 0);
675 #endif /* BFD_ASSEMBLER */
677 subseg_set (text_section
, 0);
679 /* This may add symbol table entries, which requires having an open BFD,
680 and sections already created, in BFD_ASSEMBLER mode. */
683 argv
++; /* skip argv[0] */
684 argc
--; /* skip argv[0] */
688 { /* Is it a file-name argument? */
691 /* argv->"" if stdin desired, else->filename */
692 read_a_source_file (*argv
);
694 argv
++; /* completed that argv */
697 read_a_source_file ("");
698 } /* perform_an_assembly_pass() */
700 /* The interface between the macro code and gas expression handling. */
703 macro_expr (emsg
, idx
, in
, val
)
714 hold
= input_line_pointer
;
715 input_line_pointer
= in
->ptr
+ idx
;
717 idx
= input_line_pointer
- in
->ptr
;
718 input_line_pointer
= hold
;
720 if (ex
.X_op
!= O_constant
)
723 *val
= (int) ex
.X_add_number
;