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 <sys/types.h> /* For pid_t in signal.h */
50 #include "output-file.h"
61 /* Not currently used. */
62 static SIGTY got_sig PARAMS ((int sig));
64 static void perform_an_assembly_pass PARAMS ((int argc, char **argv));
67 #define EXIT_SUCCESS 0
68 #define EXIT_FAILURE 1
71 int listing; /* true if a listing is wanted */
73 char *myname; /* argv[0] */
75 segT reg_section, expr_section;
76 segT text_section, data_section, bss_section;
79 /* This is true if the assembler should output time and space usage. */
81 static int statistics_flag = 0;
92 fprintf (stderr, "GNU assembler version %s (%s)", GAS_VERSION, TARGET_ALIAS);
94 fprintf (stderr, ", using BFD version %s", BFD_VERSION);
96 fprintf (stderr, "\n");
103 fprintf (stream, "Usage: %s [option...] [asmfile...]\n", myname);
107 -a[sub-option...] turn on listings\n\
108 Sub-options [default hls]:\n\
109 d omit debugging directives\n\
110 h include high-level source\n\
111 l include assembly\n\
112 n omit forms processing\n\
115 -f skip whitespace and comment preprocessing\n\
116 --help show this message and exit\n\
117 -I DIR add DIR to search list for .include directives\n\
118 -J don't warn about signed overflow\n\
119 -K warn when differences altered for long displacements\n\
120 -L keep local symbols (starting with `L')\n");
122 -o OBJFILE name the object-file output OBJFILE [default a.out]\n\
123 -R fold data section into text section\n\
124 --statistics print maximum bytes and total seconds used\n\
125 -v, -version print assembler version number\n\
126 --version print assembler version number and exit\n\
127 -W suppress warnings\n\
130 -Z generate object file even after errors\n");
135 -32addr treat addresses as 32-bit values\n\
136 -F lack floating point instructions support\n\
143 -ACA | -ACA_A | -ACB | -ACC | -AKA | -AKB | -AKC | -AMC\n\
144 specify variant of 960 architecture\n\
145 -b add code to collect statistics about branches taken\n\
146 -linkrelax make relocatable instructions undefined (?)\n\
147 -norelax don't alter compare-and-branch instructions for\n\
148 long displacements\n");
154 -l use 1 word for refs to undefined symbols [default 2]\n\
155 -m68000 | -m68008 | -m68010 | -m68020 | -m68030 | -m68040\n\
156 | -m68302 | -m68331 | -m68332 | -m68333 | -m68340 | -mcpu32\n\
157 specify variant of 680X0 architecture [default 68020]\n\
158 -m68881 | -m68882 | -mno-68881 | -mno-68882\n\
159 target has/lacks floating-point coprocessor\n\
160 [default yes for 68020, 68030, and cpu32]\n\
161 -m68851 | -mno-68851\n\
162 target has/lacks memory-management unit coprocessor\n\
163 [default yes for 68020 and up]\n\
164 -pic, -k (sun3) generate position independent code\n\
165 -S turn jbsr into jsr\n\
166 --register-prefix-optional\n\
167 recognize register names without prefix character\n");
173 -membedded-pic generate embedded position independent code\n\
175 -EB generate big endian output\n\
176 -EL generate little endian output\n\
177 -g, -g2 do not remove uneeded NOPs or swap branches\n\
178 -G NUM allow referencing objects up to NUM bytes\n\
179 implicitly with the gp register [default 8]\n\
180 -mips1, -mcpu=r{2,3}000 generate code for r2000 and r3000\n\
181 -mips2, -mcpu=r6000 generate code for r6000\n\
182 -mips3, -mcpu=r4000 generate code for r4000\n\
183 -O0 remove unneeded NOPs, do not swap branches\n\
184 -O remove unneeded NOPs and swap branches\n\
185 --trap, --no-break trap exception on div by 0 and mult overflow\n\
186 --break, --no-trap break exception on div by 0 and mult overflow\n");
190 -KPIC, -call_shared generate SVR4 position independent code\n\
191 -non_shared do not generate position independent code\n");
198 -m32032 | -m32532 select variant of NS32K architecture\n");
205 -mpwrx generate code for IBM POWER/2 (RIOS2)\n\
206 -mpwr generate code for IBM POWER (RIOS1)\n\
207 -m601 generate code for Motorola PowerPC 601\n\
208 -mppc generate code for Motorola PowerPC 603/604\n\
209 -many generate code for any architecture (PWR/PWRX/PPC)\n");
212 PowerPC ELF options:\n\
213 -V print assembler version number\n\
214 -Qy, -Qn ignored\n");
221 -relax alter jump instructions for long displacements\n");
227 -Av6 | -Av7 | -Av8 | -Asparclite\n\
228 specify variant of SPARC architecture\n\
229 -bump warn when assembler switches architectures\n\
233 SPARC ELF options:\n\
234 -V print assembler version number\n\
267 -z8001 generate segmented code\n\
268 -z8002 generate unsegmented code\n");
273 * Parse arguments, but we are only interested in flags.
274 * When we find a flag, we process it then make it's argv[] NULL.
275 * This helps any future argv[] scanners avoid what we processed.
276 * Since it is easy to do here we interpret the special arg "-"
277 * to mean "use stdin" and we set that argv[] pointing to "".
278 * After we have munged argv[], the only things left are source file
279 * name(s) and ""(s) denoting stdin. These file names are used
280 * (perhaps more than once) later.
282 /* FIXME-SOMEDAY this should use getopt. */
284 * check for new machine-dep cmdline options in
285 * md_parse_option definitions in config/tc-*.c
289 parse_args (argc, argv)
293 char *arg; /* an arg to program */
294 char a; /* an arg flag (after -) */
296 argc--; /* don't count argv[0] */
297 argv++; /* skip argv[0] */
299 for (; argc--; argv++)
301 arg = *argv; /* argv points to this argument */
303 if (*arg != '-') /* Filename. We need it later. */
304 continue; /* Keep scanning args looking for flags. */
305 /* Handle double-dash options. */
310 /* "--" as an argument means read stdin. */
311 /* On this scan, we don't want to think about filenames. */
312 *argv = ""; /* A code that means 'use stdin'. */
314 else if (strcmp (arg, "--statistics") == 0)
319 else if (strcmp (arg, "--help") == 0)
324 else if (strcmp (arg, "--version") == 0)
329 #ifdef md_parse_long_option
330 else if (md_parse_long_option (arg))
335 as_warn ("Unknown option `%s' ignored", arg);
341 /* This better be a switch. */
342 arg++; /*->letter. */
344 while ((a = *arg) != '\0')
345 { /* scan all the 1-char flags */
346 arg++; /* arg->after letter. */
347 a &= 0x7F; /* ascii only please */
348 flagseen[(unsigned char) a] = 1;
360 listing |= LISTING_LISTING;
364 listing |= LISTING_SYMBOLS;
368 listing |= LISTING_HLL;
373 listing |= LISTING_NOFORM;
377 listing |= LISTING_NODEBUG;
382 listing = LISTING_DEFAULT;
393 break; /* -f means fast - no need for "app" preprocessor. */
396 /* DEBUG is implemented: it debugs different */
397 /* things to other people's assemblers. */
401 { /* Include file directory */
408 as_fatal ("virtual memory exhausted");
417 as_warn ("%s: I expected a filename after -I", myname);
418 add_include_dir (temp);
419 arg = ""; /* Finished with this arg. */
423 #ifdef WARN_SIGNED_OVERFLOW_WORD
424 /* Don't warn about signed overflow. */
429 #ifndef WORKING_DOT_WORD
434 case 'L': /* -L means keep L* symbols */
438 if (*arg) /* Rest of argument is object file-name. */
440 out_file_name = strdup (arg);
442 as_fatal ("virtual memory exhausted");
445 { /* Want next arg for a file-name. */
446 *argv = NULL; /* This is not a file-name. */
448 out_file_name = *++argv;
451 as_warn ("%s: I expected a filename after -o. \"%s\" assumed.",
452 myname, out_file_name);
453 arg = ""; /* Finished with this arg. */
457 if (*arg && strcmp(arg, "ocpp") == 0)
461 as_warn ("Unknown option `-n%s' ignored", arg);
467 /* -R means put data into text segment */
468 flag_readonly_data_in_text = 1;
474 extern char *compiler_version_string;
475 compiler_version_string = arg;
478 if (*arg && strcmp (arg, "ersion"))
480 as_warn ("Unknown option `-v%s' ignored", arg);
488 arg++; /* Skip the rest */
492 /* -W means don't warn about things */
493 flag_suppress_warnings = 1;
498 /* -X means treat warnings as errors */
501 /* -Z means attempt to generate object file even after errors. */
502 flag_always_generate_output = 1;
507 if (md_parse_option (&arg, &argc, &argv) == 0)
508 as_warn ("%s: I don't understand '%c' flag.", myname, a);
515 * We have just processed a "-..." arg, which was not a
516 * file-name. Smash it so the
517 * things that look for filenames won't ever see it.
519 * Whatever argv points to, it has already been used
520 * as part of a flag, so DON'T re-use it as a filename.
522 *argv = NULL; /* NULL means 'not a file-name' */
533 long start_time = get_run_time ();
535 #if 0 /* do we need any of this?? */
537 static const int sig[] = {SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
539 for (a = 0; sig[a] != 0; a++)
540 if (signal (sig[a], SIG_IGN) != SIG_IGN)
541 signal (sig[a], got_sig);
546 memset (flagseen, '\0', sizeof (flagseen)); /* aint seen nothing yet */
547 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
548 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
550 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
559 input_scrub_begin ();
561 parse_args (argc, argv);
564 output_file_create (out_file_name);
565 assert (stdoutput != 0);
568 #ifdef tc_init_after_args
569 tc_init_after_args ();
572 /* Here with flags set up in flagseen[]. */
573 perform_an_assembly_pass (argc, argv); /* Assemble it. */
578 if (seen_at_least_1_file ()
579 && !((had_warnings () && flag_always_generate_output)
580 || had_errors () > 0))
586 write_object_file ();
592 #ifndef OBJ_VMS /* does its own file handling */
593 #ifndef BFD_ASSEMBLER
596 output_file_close (out_file_name);
600 unlink (out_file_name);
609 extern char **environ;
610 char *lim = (char *) sbrk (0);
611 long run_time = get_run_time () - start_time;
613 fprintf (stderr, "%s: total time in assembly: %ld.%06ld\n",
614 myname, run_time / 1000000, run_time % 1000000);
615 fprintf (stderr, "%s: data size %ld\n",
616 myname, (long) (lim - (char *) &environ));
619 if ((had_warnings () && flagseen['Z'])
620 || had_errors () > 0)
626 /* perform_an_assembly_pass()
628 * Here to attempt 1 pass over each input file.
629 * We scan argv[*] looking for filenames or exactly "" which is
630 * shorthand for stdin. Any argv that is NULL is not a file-name.
631 * We set need_pass_2 TRUE if, after this, we still have unresolved
632 * expressions of the form (unknown value)+-(unknown value).
634 * Note the un*x semantics: there is only 1 logical input file, but it
635 * may be a catenation of many 'physical' input files.
638 perform_an_assembly_pass (argc, argv)
649 #ifndef BFD_ASSEMBLER
653 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
654 segment_info[i].fix_root = 0;
656 /* Create the three fixed ones */
661 seg = subseg_new (".wtext", 0);
663 seg = subseg_new (".text", 0);
665 assert (seg == SEG_E0);
666 seg = subseg_new (".data", 0);
667 assert (seg == SEG_E1);
668 seg = subseg_new (".bss", 0);
669 assert (seg == SEG_E2);
671 create_target_segments ();
675 #else /* not MANY_SEGMENTS */
676 text_fix_root = NULL;
677 data_fix_root = NULL;
679 #endif /* not MANY_SEGMENTS */
680 #else /* BFD_ASSEMBLER */
681 /* Create the standard sections, and those the assembler uses
683 text_section = subseg_new (".text", 0);
684 data_section = subseg_new (".data", 0);
685 bss_section = subseg_new (".bss", 0);
686 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
687 to have relocs, otherwise we don't find out in time. */
688 applicable = bfd_applicable_section_flags (stdoutput);
689 bfd_set_section_flags (stdoutput, text_section,
690 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
691 | SEC_CODE | SEC_READONLY));
692 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
693 bfd_set_section_flags (stdoutput, data_section,
694 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC));
695 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
696 seg_info (bss_section)->bss = 1;
697 subseg_new (BFD_ABS_SECTION_NAME, 0);
698 subseg_new (BFD_UND_SECTION_NAME, 0);
699 reg_section = subseg_new ("*GAS `reg' section*", 0);
700 expr_section = subseg_new ("*GAS `expr' section*", 0);
702 #endif /* BFD_ASSEMBLER */
704 subseg_set (text_section, 0);
706 /* This may add symbol table entries, which requires having an open BFD,
707 and sections already created, in BFD_ASSEMBLER mode. */
710 argv++; /* skip argv[0] */
711 argc--; /* skip argv[0] */
715 { /* Is it a file-name argument? */
717 /* argv->"" if stdin desired, else->filename */
718 read_a_source_file (*argv);
720 argv++; /* completed that argv */
723 read_a_source_file ("");
724 } /* perform_an_assembly_pass() */
727 /* This is not currently used. */
732 static here_before = 0;
734 as_bad ("Interrupted by signal %d", sig);
737 #if 0 /* If SIGTY is void, this produces warnings. */