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"
55 /* Not currently used. */
56 static SIGTY got_sig
PARAMS ((int sig
));
58 static void perform_an_assembly_pass
PARAMS ((int argc
, char **argv
));
60 int listing
; /* true if a listing is wanted */
62 char *myname
; /* argv[0] */
64 segT reg_section
, expr_section
;
65 segT text_section
, data_section
, bss_section
;
77 fprintf (stderr
, "GNU assembler version %s (%s)", GAS_VERSION
, TARGET_ALIAS
);
79 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
81 fprintf (stderr
, "\n");
88 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
92 -a[sub-option...] turn on listings\n\
93 Sub-options [default hls]:\n\
94 d omit debugging directives\n\
95 h include high-level source\n\
97 n omit forms processing\n\
98 s include symbols\n");
100 -D produce assembler debugging messages\n\
101 -f skip whitespace and comment preprocessing\n\
102 --help show this message and exit\n\
103 -I DIR add DIR to search list for .include directives\n\
104 -J don't warn about signed overflow\n\
105 -K warn when differences altered for long displacements\n\
106 -L keep local symbols (starting with `L')\n");
109 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
110 -R fold data section into text section\n\
111 --statistics print maximum bytes and total seconds used\n\
112 --version print assembler version number and exit\n\
113 -W suppress warnings\n\
116 -Z generate object file even after errors\n");
118 md_show_usage (stream
);
122 * Since it is easy to do here we interpret the special arg "-"
123 * to mean "use stdin" and we set that argv[] pointing to "".
124 * After we have munged argv[], the only things left are source file
125 * name(s) and ""(s) denoting stdin. These file names are used
126 * (perhaps more than once) later.
128 * check for new machine-dep cmdline options in
129 * md_parse_option definitions in config/tc-*.c
133 parse_args (pargc
, pargv
)
137 int old_argc
, new_argc
;
138 char **old_argv
, **new_argv
;
140 /* Starting the short option string with '-' is for programs that
141 expect options and other ARGV-elements in any order and that care about
142 the ordering of the two. We describe each non-option ARGV-element
143 as if it were the argument of an option with character code 1. */
146 extern CONST
char *md_shortopts
;
148 /* -v takes an argument on VMS, so we don't make it a generic option. */
149 CONST
char *std_shortopts
= "-JKLRWZfa::DI:o:wX";
151 CONST
char *std_shortopts
= "-JKLRWZfa::DI:o:vwX";
154 struct option
*longopts
;
155 extern struct option md_longopts
[];
156 extern size_t md_longopts_size
;
157 static const struct option std_longopts
[] = {
158 #define OPTION_HELP (OPTION_STD_BASE)
159 {"help", no_argument
, NULL
, OPTION_HELP
},
160 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
161 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
162 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
163 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
164 #define OPTION_VERSION (OPTION_STD_BASE + 3)
165 {"version", no_argument
, NULL
, OPTION_VERSION
},
166 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
167 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
168 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
169 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
172 /* Construct the option lists from the standard list and the
173 target dependent list. */
174 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
175 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
176 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
177 memcpy ((char *) longopts
+ sizeof (std_longopts
),
178 md_longopts
, md_longopts_size
);
180 /* Make a local copy of the old argv. */
184 /* Initialize a new argv that contains no options. */
185 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
186 new_argv
[0] = old_argv
[0];
188 new_argv
[new_argc
] = NULL
;
192 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
193 indicate a long option. */
195 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
204 /* md_parse_option should return 1 if it recognizes optc,
206 if (md_parse_option (optc
, optarg
) != 0)
208 /* `-v' isn't included in the general short_opts list, so check for
209 it explicity here before deciding we've gotten a bad argument. */
213 /* Telling getopt to treat -v's value as optional can result
214 in it picking up a following filename argument here. The
215 VMS code in md_parse_option can return 0 in that case,
216 but it has no way of pushing the filename argument back. */
217 if (optarg
&& *optarg
)
218 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
232 case 1: /* File name. */
233 if (!strcmp (optarg
, "-"))
235 new_argv
[new_argc
++] = optarg
;
236 new_argv
[new_argc
] = NULL
;
246 case OPTION_STATISTICS
:
247 flag_print_statistics
= 1;
254 case OPTION_DUMPCONFIG
:
255 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
256 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
257 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
258 #ifdef TARGET_OBJ_FORMAT
259 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
262 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
267 flag_signed_overflow_ok
= 1;
271 flag_warn_displacement
= 1;
275 flag_keep_locals
= 1;
279 flag_readonly_data_in_text
= 1;
283 flag_no_warnings
= 1;
287 flag_always_generate_output
= 1;
298 listing
|= LISTING_NODEBUG
;
301 listing
|= LISTING_HLL
;
304 listing
|= LISTING_LISTING
;
307 listing
|= LISTING_NOFORM
;
310 listing
|= LISTING_SYMBOLS
;
313 as_fatal ("invalid listing option `%c'", *optarg
);
320 listing
= LISTING_DEFAULT
;
324 /* DEBUG is implemented: it debugs different */
325 /* things from other people's assemblers. */
330 flag_no_comments
= 1;
334 { /* Include file directory */
335 char *temp
= strdup (optarg
);
337 as_fatal ("virtual memory exhausted");
338 add_include_dir (temp
);
343 out_file_name
= strdup (optarg
);
345 as_fatal ("virtual memory exhausted");
352 /* -X means treat warnings as errors */
370 long start_time
= get_run_time ();
372 #ifdef HOST_SPECIAL_INIT
373 HOST_SPECIAL_INIT (argc
, argv
);
376 #if 0 /* do we need any of this?? */
378 static const int sig
[] = {SIGHUP
, SIGINT
, SIGPIPE
, SIGTERM
, 0};
381 for (a
= 0; sig
[a
] != 0; a
++)
382 if (signal (sig
[a
], SIG_IGN
) != SIG_IGN
)
383 signal (sig
[a
], got_sig
);
388 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
389 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
391 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
400 input_scrub_begin ();
402 parse_args (&argc
, &argv
);
405 output_file_create (out_file_name
);
406 assert (stdoutput
!= 0);
409 #ifdef tc_init_after_args
410 tc_init_after_args ();
413 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
417 /* start-sanitize-rce */
421 /* end-sanitize-rce */
423 if (seen_at_least_1_file ()
424 && !((had_warnings () && flag_always_generate_output
)
425 || had_errors () > 0))
431 write_object_file ();
437 #ifndef OBJ_VMS /* does its own file handling */
438 #ifndef BFD_ASSEMBLER
441 output_file_close (out_file_name
);
445 unlink (out_file_name
);
452 if (flag_print_statistics
)
454 extern char **environ
;
455 char *lim
= (char *) sbrk (0);
456 long run_time
= get_run_time () - start_time
;
458 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
459 myname
, run_time
/ 1000000, run_time
% 1000000);
460 fprintf (stderr
, "%s: data size %ld\n",
461 myname
, (long) (lim
- (char *) &environ
));
464 /* Use exit instead of return, because under VMS environments they
465 may not place the same interpretation on the value given. */
466 if ((had_warnings () && flag_always_generate_output
)
467 || had_errors () > 0)
473 /* perform_an_assembly_pass()
475 * Here to attempt 1 pass over each input file.
476 * We scan argv[*] looking for filenames or exactly "" which is
477 * shorthand for stdin. Any argv that is NULL is not a file-name.
478 * We set need_pass_2 TRUE if, after this, we still have unresolved
479 * expressions of the form (unknown value)+-(unknown value).
481 * Note the un*x semantics: there is only 1 logical input file, but it
482 * may be a catenation of many 'physical' input files.
485 perform_an_assembly_pass (argc
, argv
)
496 #ifndef BFD_ASSEMBLER
500 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
501 segment_info
[i
].fix_root
= 0;
503 /* Create the three fixed ones */
508 seg
= subseg_new (".wtext", 0);
510 seg
= subseg_new (".text", 0);
512 assert (seg
== SEG_E0
);
513 seg
= subseg_new (".data", 0);
514 assert (seg
== SEG_E1
);
515 seg
= subseg_new (".bss", 0);
516 assert (seg
== SEG_E2
);
518 create_target_segments ();
522 #else /* not MANY_SEGMENTS */
523 text_fix_root
= NULL
;
524 data_fix_root
= NULL
;
526 #endif /* not MANY_SEGMENTS */
527 #else /* BFD_ASSEMBLER */
528 /* Create the standard sections, and those the assembler uses
530 text_section
= subseg_new (".text", 0);
531 data_section
= subseg_new (".data", 0);
532 bss_section
= subseg_new (".bss", 0);
533 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
534 to have relocs, otherwise we don't find out in time. */
535 applicable
= bfd_applicable_section_flags (stdoutput
);
536 bfd_set_section_flags (stdoutput
, text_section
,
537 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
538 | SEC_CODE
| SEC_READONLY
));
539 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
540 bfd_set_section_flags (stdoutput
, data_section
,
541 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
542 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
543 seg_info (bss_section
)->bss
= 1;
544 subseg_new (BFD_ABS_SECTION_NAME
, 0);
545 subseg_new (BFD_UND_SECTION_NAME
, 0);
546 reg_section
= subseg_new ("*GAS `reg' section*", 0);
547 expr_section
= subseg_new ("*GAS `expr' section*", 0);
549 #endif /* BFD_ASSEMBLER */
551 subseg_set (text_section
, 0);
553 /* This may add symbol table entries, which requires having an open BFD,
554 and sections already created, in BFD_ASSEMBLER mode. */
557 argv
++; /* skip argv[0] */
558 argc
--; /* skip argv[0] */
562 { /* Is it a file-name argument? */
564 /* argv->"" if stdin desired, else->filename */
565 read_a_source_file (*argv
);
567 argv
++; /* completed that argv */
570 read_a_source_file ("");
571 } /* perform_an_assembly_pass() */
574 /* This is not currently used. */
579 static here_before
= 0;
581 as_bad ("Interrupted by signal %d", sig
);
584 #if 0 /* If SIGTY is void, this produces warnings. */