1 /* as.c - GAS main program.
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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
32 * Since no-one else says they will support them in future: I
33 * don't support them now.
43 #include "output-file.h"
47 #define itbl_parse(itbl_file) 1
52 #ifdef NEED_DECLARATION_SBRK
57 static void show_usage
PARAMS ((FILE *));
58 static void parse_args
PARAMS ((int *, char ***));
59 static void dump_statistics
PARAMS ((void));
60 static void perform_an_assembly_pass
PARAMS ((int argc
, char **argv
));
61 static int macro_expr
PARAMS ((const char *, int, sb
*, int *));
63 int listing
; /* true if a listing is wanted */
65 static char *listing_filename
= NULL
; /* Name of listing file. */
67 /* Type of debugging to generate. */
69 enum debug_info_type debug_type
= DEBUG_NONE
;
71 /* Maximum level of macro nesting. */
73 int max_macro_nest
= 100;
75 char *myname
; /* argv[0] */
77 segT reg_section
, expr_section
;
78 segT text_section
, data_section
, bss_section
;
83 /* To monitor memory allocation more effectively, make this non-zero.
84 Then the chunk sizes for gas and bfd will be reduced. */
87 /* We build a list of defsyms as we read the options, and then define
88 them after we have initialized everything. */
92 struct defsym_list
*next
;
97 static struct defsym_list
*defsyms
;
99 /* Keep a record of the itbl files we read in. */
101 struct itbl_file_list
103 struct itbl_file_list
*next
;
107 static struct itbl_file_list
*itbl_files
;
117 fprintf (stderr
, "GNU assembler version %s (%s)", VERSION
, TARGET_ALIAS
);
119 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
121 fprintf (stderr
, "\n");
128 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
132 -a[sub-option...] turn on listings\n\
133 Sub-options [default hls]:\n\
134 c omit false conditionals\n\
135 d omit debugging directives\n\
136 h include high-level source\n\
137 l include assembly\n\
138 n omit forms processing\n\
140 =file set listing file name (must be last sub-option)\n");
142 -D produce assembler debugging messages\n\
143 --defsym SYM=VAL define symbol SYM to given value\n\
144 -f skip whitespace and comment preprocessing\n\
145 --gstabs generate stabs debugging information\n\
146 --help show this message and exit\n\
147 -I DIR add DIR to search list for .include directives\n\
148 -J don't warn about signed overflow\n\
149 -K warn when differences altered for long displacements\n\
150 -L keep local symbols (starting with `L')\n");
152 -M,--mri assemble in MRI compatibility mode\n\
153 --MD FILE write dependency information in FILE (default none)\n\
155 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
156 -R fold data section into text section\n\
157 --statistics print various measured statistics from execution\n\
158 --version print assembler version number and exit\n\
159 -W suppress warnings\n\
160 --itbl INSTTBL extend instruction set to include instructions\n\
161 matching the specifications defined in file INSTTBL\n\
164 -Z generate object file even after errors\n");
166 md_show_usage (stream
);
168 fprintf (stream
, "\nReport bugs to bug-gnu-utils@prep.ai.mit.edu\n");
171 #ifdef USE_EMULATIONS
172 #define EMULATION_ENVIRON "AS_EMULATION"
174 extern struct emulation mipsbelf
, mipslelf
, mipself
;
175 extern struct emulation mipsbecoff
, mipslecoff
, mipsecoff
;
176 extern struct emulation i386coff
, i386elf
;
178 static struct emulation
*const emulations
[] = { EMULATIONS
};
179 static const int n_emulations
= sizeof (emulations
) / sizeof (emulations
[0]);
181 static void select_emulation_mode
PARAMS ((int, char **));
184 select_emulation_mode (argc
, argv
)
191 for (i
= 1; i
< argc
; i
++)
192 if (!strncmp ("--em", argv
[i
], 4))
198 p
= strchr (argv
[i
], '=');
205 as_fatal ("missing emulation mode name");
210 em
= getenv (EMULATION_ENVIRON
);
212 em
= DEFAULT_EMULATION
;
216 for (i
= 0; i
< n_emulations
; i
++)
217 if (!strcmp (emulations
[i
]->name
, em
))
219 if (i
== n_emulations
)
220 as_fatal ("unrecognized emulation name `%s'", em
);
221 this_emulation
= emulations
[i
];
224 this_emulation
= emulations
[0];
226 this_emulation
->init ();
230 default_emul_bfd_name ()
239 this_format
= this_emulation
->format
;
241 if (this_emulation
->leading_underscore
== 2)
242 this_emulation
->leading_underscore
= this_format
->dfl_leading_underscore
;
244 if (this_emulation
->default_endian
!= 2)
245 target_big_endian
= this_emulation
->default_endian
;
247 if (this_emulation
->fake_label_name
== 0)
249 if (this_emulation
->leading_underscore
)
250 this_emulation
->fake_label_name
= "L0\001";
252 /* What other parameters should we test? */
253 this_emulation
->fake_label_name
= ".L0\001";
259 * Since it is easy to do here we interpret the special arg "-"
260 * to mean "use stdin" and we set that argv[] pointing to "".
261 * After we have munged argv[], the only things left are source file
262 * name(s) and ""(s) denoting stdin. These file names are used
263 * (perhaps more than once) later.
265 * check for new machine-dep cmdline options in
266 * md_parse_option definitions in config/tc-*.c
270 parse_args (pargc
, pargv
)
274 int old_argc
, new_argc
;
275 char **old_argv
, **new_argv
;
277 /* Starting the short option string with '-' is for programs that
278 expect options and other ARGV-elements in any order and that care about
279 the ordering of the two. We describe each non-option ARGV-element
280 as if it were the argument of an option with character code 1. */
283 extern CONST
char *md_shortopts
;
284 static const char std_shortopts
[] =
287 #ifndef WORKING_DOT_WORD
288 /* -K is not meaningful if .word is not being hacked. */
291 'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
293 /* -v takes an argument on VMS, so we don't make it a generic
298 /* New option for extending instruction set (see also --itbl below) */
302 struct option
*longopts
;
303 extern struct option md_longopts
[];
304 extern size_t md_longopts_size
;
305 static const struct option std_longopts
[] = {
306 #define OPTION_HELP (OPTION_STD_BASE)
307 {"help", no_argument
, NULL
, OPTION_HELP
},
308 {"mri", no_argument
, NULL
, 'M'},
309 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
310 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
311 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
312 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
313 #define OPTION_VERSION (OPTION_STD_BASE + 3)
314 {"version", no_argument
, NULL
, OPTION_VERSION
},
315 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
316 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
317 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
318 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
319 #define OPTION_EMULATION (OPTION_STD_BASE + 6)
320 {"emulation", required_argument
, NULL
, OPTION_EMULATION
},
321 #define OPTION_DEFSYM (OPTION_STD_BASE + 7)
322 {"defsym", required_argument
, NULL
, OPTION_DEFSYM
},
323 #define OPTION_INSTTBL (OPTION_STD_BASE + 8)
324 /* New option for extending instruction set (see also -t above).
325 The "-t file" or "--itbl file" option extends the basic set of
326 valid instructions by reading "file", a text file containing a
327 list of instruction formats. The additional opcodes and their
328 formats are added to the built-in set of instructions, and
329 mnemonics for new registers may also be defined. */
330 {"itbl", required_argument
, NULL
, OPTION_INSTTBL
},
331 #define OPTION_DEPFILE (OPTION_STD_BASE + 9)
332 {"MD", required_argument
, NULL
, OPTION_DEPFILE
},
333 #define OPTION_GSTABS (OPTION_STD_BASE + 10)
334 {"gstabs", no_argument
, NULL
, OPTION_GSTABS
}
337 /* Construct the option lists from the standard list and the
338 target dependent list. */
339 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
340 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
341 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
342 memcpy ((char *) longopts
+ sizeof (std_longopts
),
343 md_longopts
, md_longopts_size
);
345 /* Make a local copy of the old argv. */
349 /* Initialize a new argv that contains no options. */
350 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
351 new_argv
[0] = old_argv
[0];
353 new_argv
[new_argc
] = NULL
;
357 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
358 indicate a long option. */
360 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
369 /* md_parse_option should return 1 if it recognizes optc,
371 if (md_parse_option (optc
, optarg
) != 0)
373 /* `-v' isn't included in the general short_opts list, so check for
374 it explicity here before deciding we've gotten a bad argument. */
378 /* Telling getopt to treat -v's value as optional can result
379 in it picking up a following filename argument here. The
380 VMS code in md_parse_option can return 0 in that case,
381 but it has no way of pushing the filename argument back. */
382 if (optarg
&& *optarg
)
383 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
397 case 1: /* File name. */
398 if (!strcmp (optarg
, "-"))
400 new_argv
[new_argc
++] = optarg
;
401 new_argv
[new_argc
] = NULL
;
411 case OPTION_STATISTICS
:
412 flag_print_statistics
= 1;
416 /* This output is intended to follow the GNU standards document. */
417 printf ("GNU assembler %s\n", VERSION
);
418 printf ("Copyright 1997 Free Software Foundation, Inc.\n");
420 This program is free software; you may redistribute it under the terms of\n\
421 the GNU General Public License. This program has absolutely no warranty.\n");
422 printf ("This assembler was configured for a target of `%s'.\n",
426 case OPTION_EMULATION
:
427 #ifdef USE_EMULATIONS
428 if (strcmp (optarg
, this_emulation
->name
))
429 as_fatal ("multiple emulation names specified");
431 as_fatal ("emulations not handled in this configuration");
435 case OPTION_DUMPCONFIG
:
436 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
437 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
438 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
439 #ifdef TARGET_OBJ_FORMAT
440 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
443 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
451 struct defsym_list
*n
;
453 for (s
= optarg
; *s
!= '\0' && *s
!= '='; s
++)
456 as_fatal ("bad defsym; format is --defsym name=value");
458 i
= strtol (s
, (char **) NULL
, 0);
459 n
= (struct defsym_list
*) xmalloc (sizeof *n
);
470 /* optarg is the name of the file containing the instruction
471 formats, opcodes, register names, etc. */
472 struct itbl_file_list
*n
;
476 as_warn ( "No file name following -t option\n" );
480 n
= (struct itbl_file_list
*) xmalloc (sizeof *n
);
481 n
->next
= itbl_files
;
485 /* Parse the file and add the new instructions to our internal
486 table. If multiple instruction tables are specified, the
487 information from this table gets appended onto the existing
489 itbl_files
->name
= xstrdup (optarg
);
490 if (itbl_parse (itbl_files
->name
) != 0)
492 fprintf (stderr
, "Failed to read instruction table %s\n",
500 start_dependencies (optarg
);
504 debug_type
= DEBUG_STABS
;
508 flag_signed_overflow_ok
= 1;
511 #ifndef WORKING_DOT_WORD
513 flag_warn_displacement
= 1;
518 flag_keep_locals
= 1;
529 flag_readonly_data_in_text
= 1;
533 flag_no_warnings
= 1;
537 flag_always_generate_output
= 1;
548 listing
|= LISTING_NOCOND
;
551 listing
|= LISTING_NODEBUG
;
554 listing
|= LISTING_HLL
;
557 listing
|= LISTING_LISTING
;
560 listing
|= LISTING_NOFORM
;
563 listing
|= LISTING_SYMBOLS
;
566 listing_filename
= xstrdup (optarg
+ 1);
567 optarg
+= strlen (listing_filename
);
570 as_fatal ("invalid listing option `%c'", *optarg
);
577 listing
= LISTING_DEFAULT
;
581 /* DEBUG is implemented: it debugs different */
582 /* things from other people's assemblers. */
587 flag_no_comments
= 1;
591 { /* Include file directory */
592 char *temp
= xstrdup (optarg
);
593 add_include_dir (temp
);
598 out_file_name
= xstrdup (optarg
);
605 /* -X means treat warnings as errors */
617 static long start_time
;
628 start_time
= get_run_time ();
634 extern long _bfd_chunksize
;
640 #ifdef HOST_SPECIAL_INIT
641 HOST_SPECIAL_INIT (argc
, argv
);
645 xmalloc_set_program_name (myname
);
647 START_PROGRESS (myname
, 0);
649 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
650 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
653 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
658 bfd_set_error_program_name (myname
);
661 #ifdef USE_EMULATIONS
662 select_emulation_mode (argc
, argv
);
669 parse_args (&argc
, &argv
);
671 input_scrub_begin ();
674 if (flag_print_statistics
)
675 xatexit (dump_statistics
);
680 macro_strip_at
= flag_mri
;
683 /* For compatibility with the AMD 29K family macro assembler
689 macro_init (macro_alternate
, flag_mri
, macro_strip_at
, macro_expr
);
694 output_file_create (out_file_name
);
695 assert (stdoutput
!= 0);
698 #ifdef tc_init_after_args
699 tc_init_after_args ();
704 /* Now that we have fully initialized, and have created the output
705 file, define any symbols requested by --defsym command line
707 while (defsyms
!= NULL
)
710 struct defsym_list
*next
;
712 sym
= symbol_new (defsyms
->name
, absolute_section
, defsyms
->value
,
714 symbol_table_insert (sym
);
715 next
= defsyms
->next
;
722 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
724 cond_finish_check (-1);
730 if (seen_at_least_1_file ()
731 && (flag_always_generate_output
|| had_errors () == 0))
737 write_object_file ();
740 listing_print (listing_filename
);
743 #ifndef OBJ_VMS /* does its own file handling */
744 #ifndef BFD_ASSEMBLER
747 output_file_close (out_file_name
);
750 if (had_errors () > 0 && ! flag_always_generate_output
)
754 unlink (out_file_name
);
758 END_PROGRESS (myname
);
760 /* Use xexit instead of return, because under VMS environments they
761 may not place the same interpretation on the value given. */
762 if (had_errors () > 0)
763 xexit (EXIT_FAILURE
);
765 /* Only generate dependency file if assembler was successful. */
766 print_dependencies ();
768 xexit (EXIT_SUCCESS
);
774 extern char **environ
;
776 char *lim
= (char *) sbrk (0);
778 long run_time
= get_run_time () - start_time
;
780 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
781 myname
, run_time
/ 1000000, run_time
% 1000000);
783 fprintf (stderr
, "%s: data size %ld\n",
784 myname
, (long) (lim
- (char *) &environ
));
787 subsegs_print_statistics (stderr
);
788 write_print_statistics (stderr
);
789 symbol_print_statistics (stderr
);
790 read_print_statistics (stderr
);
792 #ifdef tc_print_statistics
793 tc_print_statistics (stderr
);
795 #ifdef obj_print_statistics
796 obj_print_statistics (stderr
);
801 /* perform_an_assembly_pass()
803 * Here to attempt 1 pass over each input file.
804 * We scan argv[*] looking for filenames or exactly "" which is
805 * shorthand for stdin. Any argv that is NULL is not a file-name.
806 * We set need_pass_2 TRUE if, after this, we still have unresolved
807 * expressions of the form (unknown value)+-(unknown value).
809 * Note the un*x semantics: there is only 1 logical input file, but it
810 * may be a catenation of many 'physical' input files.
813 perform_an_assembly_pass (argc
, argv
)
824 #ifndef BFD_ASSEMBLER
828 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
829 segment_info
[i
].fix_root
= 0;
831 /* Create the three fixed ones */
836 seg
= subseg_new (".wtext", 0);
838 seg
= subseg_new (".text", 0);
840 assert (seg
== SEG_E0
);
841 seg
= subseg_new (".data", 0);
842 assert (seg
== SEG_E1
);
843 seg
= subseg_new (".bss", 0);
844 assert (seg
== SEG_E2
);
846 create_target_segments ();
850 #else /* not MANY_SEGMENTS */
851 text_fix_root
= NULL
;
852 data_fix_root
= NULL
;
854 #endif /* not MANY_SEGMENTS */
855 #else /* BFD_ASSEMBLER */
856 /* Create the standard sections, and those the assembler uses
858 text_section
= subseg_new (".text", 0);
859 data_section
= subseg_new (".data", 0);
860 bss_section
= subseg_new (".bss", 0);
861 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
862 to have relocs, otherwise we don't find out in time. */
863 applicable
= bfd_applicable_section_flags (stdoutput
);
864 bfd_set_section_flags (stdoutput
, text_section
,
865 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
866 | SEC_CODE
| SEC_READONLY
));
867 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
868 bfd_set_section_flags (stdoutput
, data_section
,
869 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
870 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
871 seg_info (bss_section
)->bss
= 1;
872 subseg_new (BFD_ABS_SECTION_NAME
, 0);
873 subseg_new (BFD_UND_SECTION_NAME
, 0);
874 reg_section
= subseg_new ("*GAS `reg' section*", 0);
875 expr_section
= subseg_new ("*GAS `expr' section*", 0);
877 #endif /* BFD_ASSEMBLER */
879 subseg_set (text_section
, 0);
881 /* This may add symbol table entries, which requires having an open BFD,
882 and sections already created, in BFD_ASSEMBLER mode. */
889 argv
++; /* skip argv[0] */
890 argc
--; /* skip argv[0] */
894 { /* Is it a file-name argument? */
897 /* argv->"" if stdin desired, else->filename */
898 read_a_source_file (*argv
);
900 argv
++; /* completed that argv */
903 read_a_source_file ("");
904 } /* perform_an_assembly_pass() */
906 /* The interface between the macro code and gas expression handling. */
909 macro_expr (emsg
, idx
, in
, val
)
920 hold
= input_line_pointer
;
921 input_line_pointer
= in
->ptr
+ idx
;
923 idx
= input_line_pointer
- in
->ptr
;
924 input_line_pointer
= hold
;
926 if (ex
.X_op
!= O_constant
)
929 *val
= (int) ex
.X_add_number
;