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 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.
38 #include "libiberty.h"
44 #include "output-file.h"
49 #ifdef NEED_DECLARATION_SBRK
54 static void perform_an_assembly_pass
PARAMS ((int argc
, char **argv
));
55 static int macro_expr
PARAMS ((const char *, int, sb
*, int *));
57 int listing
; /* true if a listing is wanted */
59 static char *listing_filename
= NULL
; /* Name of listing file. */
61 /* Maximum level of macro nesting. */
63 int max_macro_nest
= 100;
65 char *myname
; /* argv[0] */
67 segT reg_section
, expr_section
;
68 segT text_section
, data_section
, bss_section
;
73 /* To monitor memory allocation more effectively, make this non-zero.
74 Then the chunk sizes for gas and bfd will be reduced. */
77 /* We build a list of defsyms as we read the options, and then define
78 them after we have initialized everything. */
82 struct defsym_list
*next
;
87 static struct defsym_list
*defsyms
;
97 fprintf (stderr
, "GNU assembler version %s (%s)", GAS_VERSION
, TARGET_ALIAS
);
99 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
101 fprintf (stderr
, "\n");
108 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
112 -a[sub-option...] turn on listings\n\
113 Sub-options [default hls]:\n\
114 d omit debugging directives\n\
115 h include high-level source\n\
116 l include assembly\n\
117 n omit forms processing\n\
119 =file set listing file name (must be last sub-option)\n");
121 -D produce assembler debugging messages\n\
122 --defsym SYM=VAL define symbol SYM to given value\n\
123 -f skip whitespace and comment preprocessing\n\
124 --help show this message and exit\n\
125 -I DIR add DIR to search list for .include directives\n\
126 -J don't warn about signed overflow\n\
127 -K warn when differences altered for long displacements\n\
128 -L keep local symbols (starting with `L')\n");
130 -M,--mri assemble in MRI compatibility mode\n\
132 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
133 -R fold data section into text section\n\
134 --statistics print various measured statistics from execution\n\
135 --version print assembler version number and exit\n\
136 -W suppress warnings\n\
139 -Z generate object file even after errors\n");
141 md_show_usage (stream
);
143 fprintf (stream
, "\nReport bugs to bug-gnu-utils@prep.ai.mit.edu\n");
146 #ifdef USE_EMULATIONS
147 #define EMULATION_ENVIRON "AS_EMULATION"
149 extern struct emulation mipsbelf
, mipslelf
, mipself
;
150 extern struct emulation mipsbecoff
, mipslecoff
, mipsecoff
;
152 static struct emulation
*const emulations
[] = { EMULATIONS
};
153 static const int n_emulations
= sizeof (emulations
) / sizeof (emulations
[0]);
156 select_emulation_mode (argc
, argv
)
163 for (i
= 1; i
< argc
; i
++)
164 if (!strncmp ("--em", argv
[i
], 4))
170 p
= strchr (argv
[i
], '=');
177 as_fatal ("missing emulation mode name");
182 em
= getenv (EMULATION_ENVIRON
);
184 em
= DEFAULT_EMULATION
;
188 for (i
= 0; i
< n_emulations
; i
++)
189 if (!strcmp (emulations
[i
]->name
, em
))
191 if (i
== n_emulations
)
192 as_fatal ("unrecognized emulation name `%s'", em
);
193 this_emulation
= emulations
[i
];
196 this_emulation
= emulations
[0];
198 this_emulation
->init ();
202 default_emul_bfd_name ()
211 this_format
= this_emulation
->format
;
213 if (this_emulation
->leading_underscore
== 2)
214 this_emulation
->leading_underscore
= this_format
->dfl_leading_underscore
;
216 if (this_emulation
->default_endian
!= 2)
217 target_big_endian
= this_emulation
->default_endian
;
219 if (this_emulation
->fake_label_name
== 0)
221 if (this_emulation
->leading_underscore
)
222 this_emulation
->fake_label_name
= "L0\001";
224 /* What other parameters should we test? */
225 this_emulation
->fake_label_name
= ".L0\001";
231 * Since it is easy to do here we interpret the special arg "-"
232 * to mean "use stdin" and we set that argv[] pointing to "".
233 * After we have munged argv[], the only things left are source file
234 * name(s) and ""(s) denoting stdin. These file names are used
235 * (perhaps more than once) later.
237 * check for new machine-dep cmdline options in
238 * md_parse_option definitions in config/tc-*.c
242 parse_args (pargc
, pargv
)
246 int old_argc
, new_argc
;
247 char **old_argv
, **new_argv
;
249 /* Starting the short option string with '-' is for programs that
250 expect options and other ARGV-elements in any order and that care about
251 the ordering of the two. We describe each non-option ARGV-element
252 as if it were the argument of an option with character code 1. */
255 extern CONST
char *md_shortopts
;
256 static const char std_shortopts
[] =
259 #ifndef WORKING_DOT_WORD
260 /* -K is not meaningful if .word is not being hacked. */
263 'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
265 /* -v takes an argument on VMS, so we don't make it a generic
272 struct option
*longopts
;
273 extern struct option md_longopts
[];
274 extern size_t md_longopts_size
;
275 static const struct option std_longopts
[] = {
276 #define OPTION_HELP (OPTION_STD_BASE)
277 {"help", no_argument
, NULL
, OPTION_HELP
},
278 {"mri", no_argument
, NULL
, 'M'},
279 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
280 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
281 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
282 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
283 #define OPTION_VERSION (OPTION_STD_BASE + 3)
284 {"version", no_argument
, NULL
, OPTION_VERSION
},
285 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
286 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
287 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
288 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
289 #define OPTION_EMULATION (OPTION_STD_BASE + 6)
290 {"emulation", required_argument
, NULL
, OPTION_EMULATION
},
291 #define OPTION_DEFSYM (OPTION_STD_BASE + 7)
292 {"defsym", required_argument
, NULL
, OPTION_DEFSYM
}
295 /* Construct the option lists from the standard list and the
296 target dependent list. */
297 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
298 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
299 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
300 memcpy ((char *) longopts
+ sizeof (std_longopts
),
301 md_longopts
, md_longopts_size
);
303 /* Make a local copy of the old argv. */
307 /* Initialize a new argv that contains no options. */
308 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
309 new_argv
[0] = old_argv
[0];
311 new_argv
[new_argc
] = NULL
;
315 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
316 indicate a long option. */
318 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
327 /* md_parse_option should return 1 if it recognizes optc,
329 if (md_parse_option (optc
, optarg
) != 0)
331 /* `-v' isn't included in the general short_opts list, so check for
332 it explicity here before deciding we've gotten a bad argument. */
336 /* Telling getopt to treat -v's value as optional can result
337 in it picking up a following filename argument here. The
338 VMS code in md_parse_option can return 0 in that case,
339 but it has no way of pushing the filename argument back. */
340 if (optarg
&& *optarg
)
341 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
355 case 1: /* File name. */
356 if (!strcmp (optarg
, "-"))
358 new_argv
[new_argc
++] = optarg
;
359 new_argv
[new_argc
] = NULL
;
369 case OPTION_STATISTICS
:
370 flag_print_statistics
= 1;
374 /* This output is intended to follow the GNU standards document. */
375 printf ("GNU assembler %s\n", GAS_VERSION
);
376 printf ("Copyright 1996 Free Software Foundation, Inc.\n");
378 This program is free software; you may redistribute it under the terms of\n\
379 the GNU General Public License. This program has absolutely no warranty.\n");
380 printf ("This assembler was configured for a target of `%s'.\n",
384 case OPTION_EMULATION
:
385 #ifdef USE_EMULATIONS
386 if (strcmp (optarg
, this_emulation
->name
))
387 as_fatal ("multiple emulation names specified");
389 as_fatal ("emulations not handled in this configuration");
393 case OPTION_DUMPCONFIG
:
394 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
395 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
396 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
397 #ifdef TARGET_OBJ_FORMAT
398 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
401 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
409 struct defsym_list
*n
;
411 for (s
= optarg
; *s
!= '\0' && *s
!= '='; s
++)
414 as_fatal ("bad defsym; format is --defsym name=value");
416 i
= strtol (s
, (char **) NULL
, 0);
417 n
= (struct defsym_list
*) xmalloc (sizeof *n
);
426 flag_signed_overflow_ok
= 1;
429 #ifndef WORKING_DOT_WORD
431 flag_warn_displacement
= 1;
436 flag_keep_locals
= 1;
447 flag_readonly_data_in_text
= 1;
451 flag_no_warnings
= 1;
455 flag_always_generate_output
= 1;
466 listing
|= LISTING_NODEBUG
;
469 listing
|= LISTING_HLL
;
472 listing
|= LISTING_LISTING
;
475 listing
|= LISTING_NOFORM
;
478 listing
|= LISTING_SYMBOLS
;
481 listing_filename
= xstrdup (optarg
+ 1);
482 optarg
+= strlen (listing_filename
);
485 as_fatal ("invalid listing option `%c'", *optarg
);
492 listing
= LISTING_DEFAULT
;
496 /* DEBUG is implemented: it debugs different */
497 /* things from other people's assemblers. */
502 flag_no_comments
= 1;
506 { /* Include file directory */
507 char *temp
= xstrdup (optarg
);
508 add_include_dir (temp
);
513 out_file_name
= xstrdup (optarg
);
520 /* -X means treat warnings as errors */
532 static void dump_statistics ();
533 static long start_time
;
544 start_time
= get_run_time ();
549 extern long _bfd_chunksize
;
555 #ifdef HOST_SPECIAL_INIT
556 HOST_SPECIAL_INIT (argc
, argv
);
560 xmalloc_set_program_name (myname
);
562 START_PROGRESS (myname
, 0);
564 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
565 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
568 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
573 bfd_set_error_program_name (myname
);
576 #ifdef USE_EMULATIONS
577 select_emulation_mode (argc
, argv
);
584 parse_args (&argc
, &argv
);
586 input_scrub_begin ();
589 if (flag_print_statistics
)
590 xatexit (dump_statistics
);
595 macro_strip_at
= flag_mri
;
598 /* For compatibility with the AMD 29K family macro assembler
604 macro_init (macro_alternate
, flag_mri
, macro_strip_at
, macro_expr
);
609 output_file_create (out_file_name
);
610 assert (stdoutput
!= 0);
613 #ifdef tc_init_after_args
614 tc_init_after_args ();
617 /* Now that we have fully initialized, and have created the output
618 file, define any symbols requested by --defsym command line
620 while (defsyms
!= NULL
)
623 struct defsym_list
*next
;
625 sym
= symbol_new (defsyms
->name
, absolute_section
, defsyms
->value
,
627 symbol_table_insert (sym
);
628 next
= defsyms
->next
;
635 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
641 if (seen_at_least_1_file ()
642 && !((had_warnings () && flag_always_generate_output
)
643 || had_errors () > 0))
649 write_object_file ();
652 listing_print (listing_filename
);
655 #ifndef OBJ_VMS /* does its own file handling */
656 #ifndef BFD_ASSEMBLER
659 output_file_close (out_file_name
);
663 unlink (out_file_name
);
667 END_PROGRESS (myname
);
669 /* Use xexit instead of return, because under VMS environments they
670 may not place the same interpretation on the value given. */
671 if ((had_warnings () && flag_always_generate_output
)
672 || had_errors () > 0)
673 xexit (EXIT_FAILURE
);
674 xexit (EXIT_SUCCESS
);
680 extern char **environ
;
682 char *lim
= (char *) sbrk (0);
684 long run_time
= get_run_time () - start_time
;
686 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
687 myname
, run_time
/ 1000000, run_time
% 1000000);
689 fprintf (stderr
, "%s: data size %ld\n",
690 myname
, (long) (lim
- (char *) &environ
));
693 subsegs_print_statistics (stderr
);
694 write_print_statistics (stderr
);
695 symbol_print_statistics (stderr
);
696 read_print_statistics (stderr
);
698 #ifdef tc_print_statistics
699 tc_print_statistics (stderr
);
701 #ifdef obj_print_statistics
702 obj_print_statistics (stderr
);
707 /* perform_an_assembly_pass()
709 * Here to attempt 1 pass over each input file.
710 * We scan argv[*] looking for filenames or exactly "" which is
711 * shorthand for stdin. Any argv that is NULL is not a file-name.
712 * We set need_pass_2 TRUE if, after this, we still have unresolved
713 * expressions of the form (unknown value)+-(unknown value).
715 * Note the un*x semantics: there is only 1 logical input file, but it
716 * may be a catenation of many 'physical' input files.
719 perform_an_assembly_pass (argc
, argv
)
730 #ifndef BFD_ASSEMBLER
734 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
735 segment_info
[i
].fix_root
= 0;
737 /* Create the three fixed ones */
742 seg
= subseg_new (".wtext", 0);
744 seg
= subseg_new (".text", 0);
746 assert (seg
== SEG_E0
);
747 seg
= subseg_new (".data", 0);
748 assert (seg
== SEG_E1
);
749 seg
= subseg_new (".bss", 0);
750 assert (seg
== SEG_E2
);
752 create_target_segments ();
756 #else /* not MANY_SEGMENTS */
757 text_fix_root
= NULL
;
758 data_fix_root
= NULL
;
760 #endif /* not MANY_SEGMENTS */
761 #else /* BFD_ASSEMBLER */
762 /* Create the standard sections, and those the assembler uses
764 text_section
= subseg_new (".text", 0);
765 data_section
= subseg_new (".data", 0);
766 bss_section
= subseg_new (".bss", 0);
767 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
768 to have relocs, otherwise we don't find out in time. */
769 applicable
= bfd_applicable_section_flags (stdoutput
);
770 bfd_set_section_flags (stdoutput
, text_section
,
771 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
772 | SEC_CODE
| SEC_READONLY
));
773 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
774 bfd_set_section_flags (stdoutput
, data_section
,
775 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
776 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
777 seg_info (bss_section
)->bss
= 1;
778 subseg_new (BFD_ABS_SECTION_NAME
, 0);
779 subseg_new (BFD_UND_SECTION_NAME
, 0);
780 reg_section
= subseg_new ("*GAS `reg' section*", 0);
781 expr_section
= subseg_new ("*GAS `expr' section*", 0);
783 #endif /* BFD_ASSEMBLER */
785 subseg_set (text_section
, 0);
787 /* This may add symbol table entries, which requires having an open BFD,
788 and sections already created, in BFD_ASSEMBLER mode. */
795 argv
++; /* skip argv[0] */
796 argc
--; /* skip argv[0] */
800 { /* Is it a file-name argument? */
803 /* argv->"" if stdin desired, else->filename */
804 read_a_source_file (*argv
);
806 argv
++; /* completed that argv */
809 read_a_source_file ("");
810 } /* perform_an_assembly_pass() */
812 /* The interface between the macro code and gas expression handling. */
815 macro_expr (emsg
, idx
, in
, val
)
826 hold
= input_line_pointer
;
827 input_line_pointer
= in
->ptr
+ idx
;
829 idx
= input_line_pointer
- in
->ptr
;
830 input_line_pointer
= hold
;
832 if (ex
.X_op
!= O_constant
)
835 *val
= (int) ex
.X_add_number
;