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, 675 Mass Ave, Cambridge, MA 02139, 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"
44 static void perform_an_assembly_pass
PARAMS ((int argc
, char **argv
));
46 int listing
; /* true if a listing is wanted */
48 static char *listing_filename
= NULL
; /* Name of listing file. */
50 char *myname
; /* argv[0] */
52 segT reg_section
, expr_section
;
53 segT text_section
, data_section
, bss_section
;
65 fprintf (stderr
, "GNU assembler version %s (%s)", GAS_VERSION
, TARGET_ALIAS
);
67 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
69 fprintf (stderr
, "\n");
76 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
80 -a[sub-option...] turn on listings\n\
81 Sub-options [default hls]:\n\
82 d omit debugging directives\n\
83 h include high-level source\n\
85 n omit forms processing\n\
87 =file set listing file name (must be last sub-option)\n");
89 -D produce assembler debugging messages\n\
90 -f skip whitespace and comment preprocessing\n\
91 --help show this message and exit\n\
92 -I DIR add DIR to search list for .include directives\n\
93 -J don't warn about signed overflow\n\
94 -K warn when differences altered for long displacements\n\
95 -L keep local symbols (starting with `L')\n");
98 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
99 -R fold data section into text section\n\
100 --statistics print maximum bytes and total seconds used\n\
101 --version print assembler version number and exit\n\
102 -W suppress warnings\n\
105 -Z generate object file even after errors\n");
107 md_show_usage (stream
);
110 #ifdef USE_EMULATIONS
111 #define EMULATION_ENVIRON "AS_EMULATION"
113 extern struct emulation mipsbelf
, mipslelf
, mipself
;
114 extern struct emulation mipsbecoff
, mipslecoff
, mipsecoff
;
116 static const char *emulation_name
;
117 static struct emulation
*const emulations
[] = { EMULATIONS
};
118 static const int n_emulations
= sizeof (emulations
) / sizeof (emulations
[0]);
121 select_emulation_mode (argc
, argv
)
128 for (i
= 1; i
< argc
; i
++)
129 if (!strncmp ("--em", argv
[i
], 4))
135 p
= strchr (argv
[i
], '=');
142 as_fatal ("missing emulation mode name");
147 em
= getenv (EMULATION_ENVIRON
);
149 em
= DEFAULT_EMULATION
;
153 for (i
= 0; i
< n_emulations
; i
++)
154 if (!strcmp (emulations
[i
]->name
, em
))
156 if (i
== n_emulations
)
157 as_fatal ("unrecognized emulation name `%s'", em
);
158 this_emulation
= emulations
[i
];
161 this_emulation
= emulations
[0];
163 this_emulation
->init ();
167 default_emul_bfd_name ()
175 this_format
= this_emulation
->format
;
177 if (this_emulation
->leading_underscore
== 2)
178 this_emulation
->leading_underscore
= this_format
->dfl_leading_underscore
;
180 if (this_emulation
->default_endian
!= 2)
181 target_big_endian
= this_emulation
->default_endian
;
183 if (this_emulation
->fake_label_name
== 0)
185 if (this_emulation
->leading_underscore
)
186 this_emulation
->fake_label_name
= "L0\001";
188 /* What other parameters should we test? */
189 this_emulation
->fake_label_name
= ".L0\001";
195 * Since it is easy to do here we interpret the special arg "-"
196 * to mean "use stdin" and we set that argv[] pointing to "".
197 * After we have munged argv[], the only things left are source file
198 * name(s) and ""(s) denoting stdin. These file names are used
199 * (perhaps more than once) later.
201 * check for new machine-dep cmdline options in
202 * md_parse_option definitions in config/tc-*.c
206 parse_args (pargc
, pargv
)
210 int old_argc
, new_argc
;
211 char **old_argv
, **new_argv
;
213 /* Starting the short option string with '-' is for programs that
214 expect options and other ARGV-elements in any order and that care about
215 the ordering of the two. We describe each non-option ARGV-element
216 as if it were the argument of an option with character code 1. */
219 extern CONST
char *md_shortopts
;
220 static const char std_shortopts
[] =
223 #ifndef WORKING_DOT_WORD
224 /* -K is not meaningful if .word is not being hacked. */
227 'L', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
229 /* -v takes an argument on VMS, so we don't make it a generic
236 struct option
*longopts
;
237 extern struct option md_longopts
[];
238 extern size_t md_longopts_size
;
239 static const struct option std_longopts
[] = {
240 #define OPTION_HELP (OPTION_STD_BASE)
241 {"help", no_argument
, NULL
, OPTION_HELP
},
242 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
243 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
244 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
245 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
246 #define OPTION_VERSION (OPTION_STD_BASE + 3)
247 {"version", no_argument
, NULL
, OPTION_VERSION
},
248 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
249 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
250 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
251 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
252 #define OPTION_EMULATION (OPTION_STD_BASE + 6)
253 {"emulation", required_argument
, NULL
, OPTION_EMULATION
},
256 /* Construct the option lists from the standard list and the
257 target dependent list. */
258 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
259 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
260 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
261 memcpy ((char *) longopts
+ sizeof (std_longopts
),
262 md_longopts
, md_longopts_size
);
264 /* Make a local copy of the old argv. */
268 /* Initialize a new argv that contains no options. */
269 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
270 new_argv
[0] = old_argv
[0];
272 new_argv
[new_argc
] = NULL
;
276 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
277 indicate a long option. */
279 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
288 /* md_parse_option should return 1 if it recognizes optc,
290 if (md_parse_option (optc
, optarg
) != 0)
292 /* `-v' isn't included in the general short_opts list, so check for
293 it explicity here before deciding we've gotten a bad argument. */
297 /* Telling getopt to treat -v's value as optional can result
298 in it picking up a following filename argument here. The
299 VMS code in md_parse_option can return 0 in that case,
300 but it has no way of pushing the filename argument back. */
301 if (optarg
&& *optarg
)
302 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
316 case 1: /* File name. */
317 if (!strcmp (optarg
, "-"))
319 new_argv
[new_argc
++] = optarg
;
320 new_argv
[new_argc
] = NULL
;
330 case OPTION_STATISTICS
:
331 flag_print_statistics
= 1;
338 case OPTION_EMULATION
:
339 #ifdef USE_EMULATIONS
340 if (strcmp (optarg
, this_emulation
->name
))
341 as_fatal ("multiple emulation names specified");
343 as_fatal ("emulations not handled in this configuration");
347 case OPTION_DUMPCONFIG
:
348 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
349 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
350 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
351 #ifdef TARGET_OBJ_FORMAT
352 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
355 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
360 flag_signed_overflow_ok
= 1;
363 #ifndef WORKING_DOT_WORD
365 flag_warn_displacement
= 1;
370 flag_keep_locals
= 1;
374 flag_readonly_data_in_text
= 1;
378 flag_no_warnings
= 1;
382 flag_always_generate_output
= 1;
393 listing
|= LISTING_NODEBUG
;
396 listing
|= LISTING_HLL
;
399 listing
|= LISTING_LISTING
;
402 listing
|= LISTING_NOFORM
;
405 listing
|= LISTING_SYMBOLS
;
408 listing_filename
= strdup (optarg
+ 1);
409 if (listing_filename
== NULL
)
410 as_fatal ("virtual memory exhausted");
411 optarg
+= strlen (listing_filename
);
414 as_fatal ("invalid listing option `%c'", *optarg
);
421 listing
= LISTING_DEFAULT
;
425 /* DEBUG is implemented: it debugs different */
426 /* things from other people's assemblers. */
431 flag_no_comments
= 1;
435 { /* Include file directory */
436 char *temp
= strdup (optarg
);
438 as_fatal ("virtual memory exhausted");
439 add_include_dir (temp
);
444 out_file_name
= strdup (optarg
);
446 as_fatal ("virtual memory exhausted");
453 /* -X means treat warnings as errors */
471 long start_time
= get_run_time ();
473 #ifdef HOST_SPECIAL_INIT
474 HOST_SPECIAL_INIT (argc
, argv
);
478 xmalloc_set_program_name (myname
);
480 START_PROGRESS (myname
, 0);
482 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
483 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
486 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
493 #ifdef USE_EMULATIONS
494 select_emulation_mode (argc
, argv
);
502 input_scrub_begin ();
504 parse_args (&argc
, &argv
);
509 output_file_create (out_file_name
);
510 assert (stdoutput
!= 0);
513 #ifdef tc_init_after_args
514 tc_init_after_args ();
519 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
523 /* start-sanitize-rce */
527 /* end-sanitize-rce */
529 if (seen_at_least_1_file ()
530 && !((had_warnings () && flag_always_generate_output
)
531 || had_errors () > 0))
537 write_object_file ();
540 listing_print (listing_filename
);
543 #ifndef OBJ_VMS /* does its own file handling */
544 #ifndef BFD_ASSEMBLER
547 output_file_close (out_file_name
);
551 unlink (out_file_name
);
558 END_PROGRESS (myname
);
560 if (flag_print_statistics
)
562 extern char **environ
;
564 char *lim
= (char *) sbrk (0);
566 long run_time
= get_run_time () - start_time
;
568 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
569 myname
, run_time
/ 1000000, run_time
% 1000000);
571 fprintf (stderr
, "%s: data size %ld\n",
572 myname
, (long) (lim
- (char *) &environ
));
576 /* Use exit instead of return, because under VMS environments they
577 may not place the same interpretation on the value given. */
578 if ((had_warnings () && flag_always_generate_output
)
579 || had_errors () > 0)
585 /* perform_an_assembly_pass()
587 * Here to attempt 1 pass over each input file.
588 * We scan argv[*] looking for filenames or exactly "" which is
589 * shorthand for stdin. Any argv that is NULL is not a file-name.
590 * We set need_pass_2 TRUE if, after this, we still have unresolved
591 * expressions of the form (unknown value)+-(unknown value).
593 * Note the un*x semantics: there is only 1 logical input file, but it
594 * may be a catenation of many 'physical' input files.
597 perform_an_assembly_pass (argc
, argv
)
608 #ifndef BFD_ASSEMBLER
612 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
613 segment_info
[i
].fix_root
= 0;
615 /* Create the three fixed ones */
620 seg
= subseg_new (".wtext", 0);
622 seg
= subseg_new (".text", 0);
624 assert (seg
== SEG_E0
);
625 seg
= subseg_new (".data", 0);
626 assert (seg
== SEG_E1
);
627 seg
= subseg_new (".bss", 0);
628 assert (seg
== SEG_E2
);
630 create_target_segments ();
634 #else /* not MANY_SEGMENTS */
635 text_fix_root
= NULL
;
636 data_fix_root
= NULL
;
638 #endif /* not MANY_SEGMENTS */
639 #else /* BFD_ASSEMBLER */
640 /* Create the standard sections, and those the assembler uses
642 text_section
= subseg_new (".text", 0);
643 data_section
= subseg_new (".data", 0);
644 bss_section
= subseg_new (".bss", 0);
645 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
646 to have relocs, otherwise we don't find out in time. */
647 applicable
= bfd_applicable_section_flags (stdoutput
);
648 bfd_set_section_flags (stdoutput
, text_section
,
649 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
650 | SEC_CODE
| SEC_READONLY
));
651 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
652 bfd_set_section_flags (stdoutput
, data_section
,
653 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
654 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
655 seg_info (bss_section
)->bss
= 1;
656 subseg_new (BFD_ABS_SECTION_NAME
, 0);
657 subseg_new (BFD_UND_SECTION_NAME
, 0);
658 reg_section
= subseg_new ("*GAS `reg' section*", 0);
659 expr_section
= subseg_new ("*GAS `expr' section*", 0);
661 #endif /* BFD_ASSEMBLER */
663 subseg_set (text_section
, 0);
665 /* This may add symbol table entries, which requires having an open BFD,
666 and sections already created, in BFD_ASSEMBLER mode. */
669 argv
++; /* skip argv[0] */
670 argc
--; /* skip argv[0] */
674 { /* Is it a file-name argument? */
677 /* argv->"" if stdin desired, else->filename */
678 read_a_source_file (*argv
);
680 argv
++; /* completed that argv */
683 read_a_source_file ("");
684 } /* perform_an_assembly_pass() */