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 char *myname
; /* argv[0] */
50 segT reg_section
, expr_section
;
51 segT text_section
, data_section
, bss_section
;
63 fprintf (stderr
, "GNU assembler version %s (%s)", GAS_VERSION
, TARGET_ALIAS
);
65 fprintf (stderr
, ", using BFD version %s", BFD_VERSION
);
67 fprintf (stderr
, "\n");
74 fprintf (stream
, "Usage: %s [option...] [asmfile...]\n", myname
);
78 -a[sub-option...] turn on listings\n\
79 Sub-options [default hls]:\n\
80 d omit debugging directives\n\
81 h include high-level source\n\
83 n omit forms processing\n\
84 s include symbols\n");
86 -D produce assembler debugging messages\n\
87 -f skip whitespace and comment preprocessing\n\
88 --help show this message and exit\n\
89 -I DIR add DIR to search list for .include directives\n\
90 -J don't warn about signed overflow\n\
91 -K warn when differences altered for long displacements\n\
92 -L keep local symbols (starting with `L')\n");
95 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
96 -R fold data section into text section\n\
97 --statistics print maximum bytes and total seconds used\n\
98 --version print assembler version number and exit\n\
99 -W suppress warnings\n\
102 -Z generate object file even after errors\n");
104 md_show_usage (stream
);
108 * Since it is easy to do here we interpret the special arg "-"
109 * to mean "use stdin" and we set that argv[] pointing to "".
110 * After we have munged argv[], the only things left are source file
111 * name(s) and ""(s) denoting stdin. These file names are used
112 * (perhaps more than once) later.
114 * check for new machine-dep cmdline options in
115 * md_parse_option definitions in config/tc-*.c
119 parse_args (pargc
, pargv
)
123 int old_argc
, new_argc
;
124 char **old_argv
, **new_argv
;
126 /* Starting the short option string with '-' is for programs that
127 expect options and other ARGV-elements in any order and that care about
128 the ordering of the two. We describe each non-option ARGV-element
129 as if it were the argument of an option with character code 1. */
132 extern CONST
char *md_shortopts
;
134 /* -v takes an argument on VMS, so we don't make it a generic option. */
135 CONST
char *std_shortopts
= "-JKLRWZfa::DI:o:wX";
137 CONST
char *std_shortopts
= "-JKLRWZfa::DI:o:vwX";
140 struct option
*longopts
;
141 extern struct option md_longopts
[];
142 extern size_t md_longopts_size
;
143 static const struct option std_longopts
[] = {
144 #define OPTION_HELP (OPTION_STD_BASE)
145 {"help", no_argument
, NULL
, OPTION_HELP
},
146 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
147 {"nocpp", no_argument
, NULL
, OPTION_NOCPP
},
148 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
149 {"statistics", no_argument
, NULL
, OPTION_STATISTICS
},
150 #define OPTION_VERSION (OPTION_STD_BASE + 3)
151 {"version", no_argument
, NULL
, OPTION_VERSION
},
152 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
153 {"dump-config", no_argument
, NULL
, OPTION_DUMPCONFIG
},
154 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
155 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
158 /* Construct the option lists from the standard list and the
159 target dependent list. */
160 shortopts
= concat (std_shortopts
, md_shortopts
, (char *) NULL
);
161 longopts
= (struct option
*) xmalloc (sizeof (std_longopts
) + md_longopts_size
);
162 memcpy (longopts
, std_longopts
, sizeof (std_longopts
));
163 memcpy ((char *) longopts
+ sizeof (std_longopts
),
164 md_longopts
, md_longopts_size
);
166 /* Make a local copy of the old argv. */
170 /* Initialize a new argv that contains no options. */
171 new_argv
= (char **) xmalloc (sizeof (char *) * (old_argc
+ 1));
172 new_argv
[0] = old_argv
[0];
174 new_argv
[new_argc
] = NULL
;
178 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
179 indicate a long option. */
181 int optc
= getopt_long_only (old_argc
, old_argv
, shortopts
, longopts
,
190 /* md_parse_option should return 1 if it recognizes optc,
192 if (md_parse_option (optc
, optarg
) != 0)
194 /* `-v' isn't included in the general short_opts list, so check for
195 it explicity here before deciding we've gotten a bad argument. */
199 /* Telling getopt to treat -v's value as optional can result
200 in it picking up a following filename argument here. The
201 VMS code in md_parse_option can return 0 in that case,
202 but it has no way of pushing the filename argument back. */
203 if (optarg
&& *optarg
)
204 new_argv
[new_argc
++] = optarg
, new_argv
[new_argc
] = NULL
;
218 case 1: /* File name. */
219 if (!strcmp (optarg
, "-"))
221 new_argv
[new_argc
++] = optarg
;
222 new_argv
[new_argc
] = NULL
;
232 case OPTION_STATISTICS
:
233 flag_print_statistics
= 1;
240 case OPTION_DUMPCONFIG
:
241 fprintf (stderr
, "alias = %s\n", TARGET_ALIAS
);
242 fprintf (stderr
, "canonical = %s\n", TARGET_CANONICAL
);
243 fprintf (stderr
, "cpu-type = %s\n", TARGET_CPU
);
244 #ifdef TARGET_OBJ_FORMAT
245 fprintf (stderr
, "format = %s\n", TARGET_OBJ_FORMAT
);
248 fprintf (stderr
, "bfd-target = %s\n", TARGET_FORMAT
);
253 flag_signed_overflow_ok
= 1;
257 flag_warn_displacement
= 1;
261 flag_keep_locals
= 1;
265 flag_readonly_data_in_text
= 1;
269 flag_no_warnings
= 1;
273 flag_always_generate_output
= 1;
284 listing
|= LISTING_NODEBUG
;
287 listing
|= LISTING_HLL
;
290 listing
|= LISTING_LISTING
;
293 listing
|= LISTING_NOFORM
;
296 listing
|= LISTING_SYMBOLS
;
299 as_fatal ("invalid listing option `%c'", *optarg
);
306 listing
= LISTING_DEFAULT
;
310 /* DEBUG is implemented: it debugs different */
311 /* things from other people's assemblers. */
316 flag_no_comments
= 1;
320 { /* Include file directory */
321 char *temp
= strdup (optarg
);
323 as_fatal ("virtual memory exhausted");
324 add_include_dir (temp
);
329 out_file_name
= strdup (optarg
);
331 as_fatal ("virtual memory exhausted");
338 /* -X means treat warnings as errors */
356 long start_time
= get_run_time ();
358 #ifdef HOST_SPECIAL_INIT
359 HOST_SPECIAL_INIT (argc
, argv
);
363 xmalloc_set_program_name (myname
);
365 START_PROGRESS (myname
, 0);
367 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
368 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
371 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
382 input_scrub_begin ();
384 parse_args (&argc
, &argv
);
389 output_file_create (out_file_name
);
390 assert (stdoutput
!= 0);
393 #ifdef tc_init_after_args
394 tc_init_after_args ();
399 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
403 /* start-sanitize-rce */
407 /* end-sanitize-rce */
409 if (seen_at_least_1_file ()
410 && !((had_warnings () && flag_always_generate_output
)
411 || had_errors () > 0))
417 write_object_file ();
423 #ifndef OBJ_VMS /* does its own file handling */
424 #ifndef BFD_ASSEMBLER
427 output_file_close (out_file_name
);
431 unlink (out_file_name
);
438 END_PROGRESS (myname
);
440 if (flag_print_statistics
)
442 extern char **environ
;
443 char *lim
= (char *) sbrk (0);
444 long run_time
= get_run_time () - start_time
;
446 fprintf (stderr
, "%s: total time in assembly: %ld.%06ld\n",
447 myname
, run_time
/ 1000000, run_time
% 1000000);
448 fprintf (stderr
, "%s: data size %ld\n",
449 myname
, (long) (lim
- (char *) &environ
));
452 /* Use exit instead of return, because under VMS environments they
453 may not place the same interpretation on the value given. */
454 if ((had_warnings () && flag_always_generate_output
)
455 || had_errors () > 0)
461 /* perform_an_assembly_pass()
463 * Here to attempt 1 pass over each input file.
464 * We scan argv[*] looking for filenames or exactly "" which is
465 * shorthand for stdin. Any argv that is NULL is not a file-name.
466 * We set need_pass_2 TRUE if, after this, we still have unresolved
467 * expressions of the form (unknown value)+-(unknown value).
469 * Note the un*x semantics: there is only 1 logical input file, but it
470 * may be a catenation of many 'physical' input files.
473 perform_an_assembly_pass (argc
, argv
)
484 #ifndef BFD_ASSEMBLER
488 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
489 segment_info
[i
].fix_root
= 0;
491 /* Create the three fixed ones */
496 seg
= subseg_new (".wtext", 0);
498 seg
= subseg_new (".text", 0);
500 assert (seg
== SEG_E0
);
501 seg
= subseg_new (".data", 0);
502 assert (seg
== SEG_E1
);
503 seg
= subseg_new (".bss", 0);
504 assert (seg
== SEG_E2
);
506 create_target_segments ();
510 #else /* not MANY_SEGMENTS */
511 text_fix_root
= NULL
;
512 data_fix_root
= NULL
;
514 #endif /* not MANY_SEGMENTS */
515 #else /* BFD_ASSEMBLER */
516 /* Create the standard sections, and those the assembler uses
518 text_section
= subseg_new (".text", 0);
519 data_section
= subseg_new (".data", 0);
520 bss_section
= subseg_new (".bss", 0);
521 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
522 to have relocs, otherwise we don't find out in time. */
523 applicable
= bfd_applicable_section_flags (stdoutput
);
524 bfd_set_section_flags (stdoutput
, text_section
,
525 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
526 | SEC_CODE
| SEC_READONLY
));
527 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
528 bfd_set_section_flags (stdoutput
, data_section
,
529 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
530 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
531 seg_info (bss_section
)->bss
= 1;
532 subseg_new (BFD_ABS_SECTION_NAME
, 0);
533 subseg_new (BFD_UND_SECTION_NAME
, 0);
534 reg_section
= subseg_new ("*GAS `reg' section*", 0);
535 expr_section
= subseg_new ("*GAS `expr' section*", 0);
537 #endif /* BFD_ASSEMBLER */
539 subseg_set (text_section
, 0);
541 /* This may add symbol table entries, which requires having an open BFD,
542 and sections already created, in BFD_ASSEMBLER mode. */
545 argv
++; /* skip argv[0] */
546 argc
--; /* skip argv[0] */
550 { /* Is it a file-name argument? */
553 /* argv->"" if stdin desired, else->filename */
554 read_a_source_file (*argv
);
556 argv
++; /* completed that argv */
559 read_a_source_file ("");
560 } /* perform_an_assembly_pass() */