* as.c (show_usage): New function.
[external/binutils.git] / gas / as.c
1 /* as.c - GAS main program.
2    Copyright (C) 1987, 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
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)
9    any later version.
10
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.
15
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. */
19
20 /*
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
24  * are shared.
25  *
26  *
27  *                      bugs
28  *
29  * : initialisers
30  *      Since no-one else says they will support them in future: I
31  * don't support them now.
32  *
33  */
34
35 #include "ansidecl.h"
36 #include "libiberty.h"
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #ifdef _POSIX_SOURCE
42 #include <sys/types.h>          /* For pid_t in signal.h */
43 #endif
44 #include <signal.h>
45
46 #define COMMON
47
48 #include "as.h"
49 #include "subsegs.h"
50 #include "output-file.h"
51
52 #ifndef SIGTY
53 #ifdef __STDC__
54 #define SIGTY void
55 #else
56 #define SIGTY int
57 #endif /* __STDC__ */
58 #endif /* SIGTY */
59
60 #if 0
61 /* Not currently used.  */
62 static SIGTY got_sig PARAMS ((int sig));
63 #endif
64 static void perform_an_assembly_pass PARAMS ((int argc, char **argv));
65
66 #ifndef EXIT_SUCCESS
67 #define EXIT_SUCCESS 0
68 #define EXIT_FAILURE 1
69 #endif
70
71 int listing;                    /* true if a listing is wanted */
72
73 char *myname;                   /* argv[0] */
74 #ifdef BFD_ASSEMBLER
75 segT reg_section, expr_section;
76 segT text_section, data_section, bss_section;
77 #endif
78
79 /* This is true if the assembler should output time and space usage. */
80
81 static int statistics_flag = 0;
82
83 \f
84 void
85 print_version_id ()
86 {
87   static int printed;
88   if (printed)
89     return;
90   printed = 1;
91
92   fprintf (stderr, "GNU assembler version %s (%s)", GAS_VERSION, TARGET_ALIAS);
93 #ifdef BFD_ASSEMBLER
94   fprintf (stderr, ", using BFD version %s", BFD_VERSION);
95 #endif
96   fprintf (stderr, "\n");
97 }
98
99 void
100 show_usage (stream)
101      FILE *stream;
102 {
103   fprintf (stream, "Usage: %s [option...] [asmfile...]\n", myname);
104
105   fprintf (stream, "\
106 Options:\n\
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\
113   s     include symbols\n\
114 -D                      ignored\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");
121   fprintf (stream, "\
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\
128 -w                      ignored\n\
129 -x                      ignored\n\
130 -Z                      generate object file even after errors\n");
131
132 #ifdef TC_ALPHA
133   fprintf(stream, "\
134 ALPHA options:\n\
135 -32addr                 treat addresses as 32-bit values\n\
136 -F                      lack floating point instructions support\n\
137 -nocpp                  ignored\n");
138 #endif
139
140 #ifdef TC_I960
141   fprintf(stream, "\
142 I960 options:\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");
149 #endif
150
151 #ifdef TC_M68K
152   fprintf(stream, "\
153 680X0 options:\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");
168 #endif
169
170 #ifdef TC_MIPS
171   fprintf(stream, "\
172 MIPS options:\n\
173 -membedded-pic          generate embedded position independent code\n\
174 -nocpp                  ignored\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");
187 #ifdef OBJ_ELF
188   fprintf(stream, "\
189 MIPS ELF options:\n\
190 -KPIC, -call_shared     generate SVR4 position independent code\n\
191 -non_shared             do not generate position independent code\n");
192 #endif
193 #endif
194
195 #ifdef TC_NS32K
196   fprintf(stream, "\
197 NS32K options:\n\
198 -m32032 | -m32532       select variant of NS32K architecture\n");
199 #endif
200
201 #ifdef TC_PPC
202   fprintf(stream, "\
203 PowerPC options:\n\
204 -u                      ignored\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");
210 #ifdef OBJ_ELF
211   fprintf(stream, "\
212 PowerPC ELF options:\n\
213 -V                      print assembler version number\n\
214 -Qy, -Qn                ignored\n");
215 #endif
216 #endif
217
218 #ifdef TC_SH
219   fprintf(stream, "\
220 SH options:\n\
221 -relax                  alter jump instructions for long displacements\n");
222 #endif
223
224 #ifdef TC_SPARC
225   fprintf(stream, "\
226 SPARC options:\n\
227 -Av6 | -Av7 | -Av8 | -Asparclite\n\
228                         specify variant of SPARC architecture\n\
229 -bump                   warn when assembler switches architectures\n\
230 -sparc                  ignored\n");
231 #ifdef OBJ_ELF
232   fprintf(stream, "\
233 SPARC ELF options:\n\
234 -V                      print assembler version number\n\
235 -q                      ignored\n\
236 -Qy, -Qn                ignored\n\
237 -s                      ignored\n");
238 #endif
239 #endif
240
241 #ifdef TC_TAHOE
242   fprintf(stream, "\
243 Tahoe options:\n\
244 -a                      ignored\n\
245 -d LENGTH               ignored\n\
246 -J                      ignored\n\
247 -S                      ignored\n\
248 -t FILE                 ignored\n\
249 -T                      ignored\n\
250 -V                      ignored\n");
251 #endif
252
253 #ifdef TC_VAX
254   fprintf(stream, "\
255 VAX options:\n\
256 -d LENGTH               ignored\n\
257 -J                      ignored\n\
258 -S                      ignored\n\
259 -t FILE                 ignored\n\
260 -T                      ignored\n\
261 -V                      ignored\n");
262 #endif
263
264 #ifdef TC_Z8K
265   fprintf(stream, "\
266 Z8K options:\n\
267 -z8001                  generate segmented code\n\
268 -z8002                  generate unsegmented code\n");
269 #endif
270 }
271
272 /*
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.
281  */
282 /* FIXME-SOMEDAY this should use getopt. */
283 /*
284  * check for new machine-dep cmdline options in
285  * md_parse_option definitions in config/tc-*.c
286  */
287
288 void
289 parse_args (argc, argv)
290      int argc;
291      char **argv;
292 {
293   char *arg;                    /* an arg to program */
294   char a;                       /* an arg flag (after -) */
295
296   argc--;                       /* don't count argv[0] */
297   argv++;                       /* skip argv[0] */
298
299   for (; argc--; argv++)
300     {
301       arg = *argv;              /* argv points to this argument */
302
303       if (*arg != '-')          /* Filename. We need it later. */
304         continue;               /* Keep scanning args looking for flags. */
305       /* Handle double-dash options. */
306       if (arg[1] == '-')
307         {
308           if (arg[2] == 0)
309             {
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'. */
313             }
314           else if (strcmp (arg, "--statistics") == 0)
315             {
316               statistics_flag = 1;
317               *argv = NULL;
318             }
319           else if (strcmp (arg, "--help") == 0)
320             {
321               show_usage (stdout);
322               exit (0);
323             }
324           else if (strcmp (arg, "--version") == 0)
325             {
326               print_version_id ();
327               exit (0);
328             }
329 #ifdef md_parse_long_option
330           else if (md_parse_long_option (arg))
331             *argv = NULL;
332 #endif
333           else
334             {
335               as_warn ("Unknown option `%s' ignored", arg);
336               *argv = NULL;
337             }
338           continue;
339         }
340
341       /* This better be a switch. */
342       arg++;                    /*->letter. */
343
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;
349           switch (a)
350             {
351             case 'a':
352               {
353                 int loop = 1;
354
355                 while (loop)
356                   {
357                     switch (*arg)
358                       {
359                       case 'l':
360                         listing |= LISTING_LISTING;
361                         arg++;
362                         break;
363                       case 's':
364                         listing |= LISTING_SYMBOLS;
365                         arg++;
366                         break;
367                       case 'h':
368                         listing |= LISTING_HLL;
369                         arg++;
370                         break;
371
372                       case 'n':
373                         listing |= LISTING_NOFORM;
374                         arg++;
375                         break;
376                       case 'd':
377                         listing |= LISTING_NODEBUG;
378                         arg++;
379                         break;
380                       default:
381                         if (!listing)
382                           listing = LISTING_DEFAULT;
383                         loop = 0;
384                         break;
385                       }
386                   }
387               }
388
389               break;
390
391
392             case 'f':
393               break;            /* -f means fast - no need for "app" preprocessor. */
394
395             case 'D':
396               /* DEBUG is implemented: it debugs different */
397               /* things to other people's assemblers. */
398               break;
399
400             case 'I':
401               {                 /* Include file directory */
402
403                 char *temp = NULL;
404                 if (*arg)
405                   {
406                     temp = strdup (arg);
407                     if (!temp)
408                       as_fatal ("virtual memory exhausted");
409                   }
410                 else if (argc)
411                   {
412                     *argv = NULL;
413                     argc--;
414                     temp = *++argv;
415                   }
416                 else
417                   as_warn ("%s: I expected a filename after -I", myname);
418                 add_include_dir (temp);
419                 arg = "";       /* Finished with this arg. */
420                 break;
421               }
422
423 #ifdef WARN_SIGNED_OVERFLOW_WORD
424               /* Don't warn about signed overflow.  */
425             case 'J':
426               break;
427 #endif
428
429 #ifndef WORKING_DOT_WORD
430             case 'K':
431               break;
432 #endif
433
434             case 'L':           /* -L means keep L* symbols */
435               break;
436
437             case 'o':
438               if (*arg)         /* Rest of argument is object file-name. */
439                 {
440                   out_file_name = strdup (arg);
441                   if (!out_file_name)
442                     as_fatal ("virtual memory exhausted");
443                 }
444               else if (argc)
445                 {               /* Want next arg for a file-name. */
446                   *argv = NULL; /* This is not a file-name. */
447                   argc--;
448                   out_file_name = *++argv;
449                 }
450               else
451                 as_warn ("%s: I expected a filename after -o. \"%s\" assumed.",
452                          myname, out_file_name);
453               arg = "";         /* Finished with this arg. */
454               break;
455
456             case 'n':
457               if (*arg && strcmp(arg, "ocpp") == 0)
458                 ;
459               else
460                 {
461                   as_warn ("Unknown option `-n%s' ignored", arg);
462                   arg += strlen (arg);
463                   break;
464                 }
465
466             case 'R':
467               /* -R means put data into text segment */
468               flag_readonly_data_in_text = 1;
469               break;
470
471             case 'v':
472 #ifdef  VMS
473               {
474                 extern char *compiler_version_string;
475                 compiler_version_string = arg;
476               }
477 #else /* not VMS */
478               if (*arg && strcmp (arg, "ersion"))
479                 {
480                   as_warn ("Unknown option `-v%s' ignored", arg);
481                   arg += strlen (arg);
482                   break;
483                 }
484
485               print_version_id ();
486 #endif /* not VMS */
487               while (*arg)
488                 arg++;          /* Skip the rest */
489               break;
490
491             case 'W':
492               /* -W means don't warn about things */
493               flag_suppress_warnings = 1;
494               break;
495
496             case 'w':
497             case 'X':
498               /* -X means treat warnings as errors */
499               break;
500             case 'Z':
501               /* -Z means attempt to generate object file even after errors. */
502               flag_always_generate_output = 1;
503               break;
504
505             default:
506               --arg;
507               if (md_parse_option (&arg, &argc, &argv) == 0)
508                 as_warn ("%s: I don't understand '%c' flag.", myname, a);
509               if (arg && *arg)
510                 arg++;
511               break;
512             }
513         }
514       /*
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.
518        *
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.
521        */
522       *argv = NULL;     /* NULL means 'not a file-name' */
523     }
524 }
525
526 int 
527 main (argc, argv)
528      int argc;
529      char **argv;
530 {
531   char a;
532   int keep_it;
533   long start_time = get_run_time ();
534
535 #if 0 /* do we need any of this?? */
536   {
537     static const int sig[] = {SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
538
539     for (a = 0; sig[a] != 0; a++)
540       if (signal (sig[a], SIG_IGN) != SIG_IGN)
541         signal (sig[a], got_sig);
542   }
543 #endif
544
545   myname = argv[0];
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"
549 #endif
550   out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
551
552 #ifdef BFD_ASSEMBLER
553   bfd_init ();
554 #endif
555
556   symbol_begin ();
557   subsegs_begin ();
558   read_begin ();
559   input_scrub_begin ();
560   frag_init ();
561   parse_args (argc, argv);
562
563 #ifdef BFD_ASSEMBLER
564   output_file_create (out_file_name);
565   assert (stdoutput != 0);
566 #endif
567
568 #ifdef tc_init_after_args
569   tc_init_after_args ();
570 #endif
571
572   /* Here with flags set up in flagseen[]. */
573   perform_an_assembly_pass (argc, argv);        /* Assemble it. */
574 #ifdef TC_I960
575   brtab_emit ();
576 #endif
577
578   if (seen_at_least_1_file ()
579       && !((had_warnings () && flag_always_generate_output)
580            || had_errors () > 0))
581     keep_it = 1;
582   else
583     keep_it = 0;
584
585   if (keep_it)
586     write_object_file ();
587
588 #ifndef NO_LISTING
589   listing_print ("");
590 #endif
591
592 #ifndef OBJ_VMS /* does its own file handling */
593 #ifndef BFD_ASSEMBLER
594   if (keep_it)
595 #endif
596     output_file_close (out_file_name);
597 #endif
598
599   if (!keep_it)
600     unlink (out_file_name);
601
602   input_scrub_end ();
603 #ifdef md_end
604   md_end ();
605 #endif
606
607   if (statistics_flag)
608     {
609       extern char **environ;
610       char *lim = (char *) sbrk (0);
611       long run_time = get_run_time () - start_time;
612
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));
617     }
618
619   if ((had_warnings () && flagseen['Z'])
620       || had_errors () > 0)
621     return EXIT_FAILURE;
622   return EXIT_SUCCESS;
623 }
624 \f
625
626 /*                      perform_an_assembly_pass()
627  *
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).
633  *
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.
636  */
637 static void 
638 perform_an_assembly_pass (argc, argv)
639      int argc;
640      char **argv;
641 {
642   int saw_a_file = 0;
643 #ifdef BFD_ASSEMBLER
644   flagword applicable;
645 #endif
646
647   need_pass_2 = 0;
648
649 #ifndef BFD_ASSEMBLER
650 #ifdef MANY_SEGMENTS
651   {
652     unsigned int i;
653     for (i = SEG_E0; i < SEG_UNKNOWN; i++)
654       segment_info[i].fix_root = 0;
655   }
656   /* Create the three fixed ones */
657   {
658     segT seg;
659
660 #ifdef TE_APOLLO
661     seg = subseg_new (".wtext", 0);
662 #else
663     seg = subseg_new (".text", 0);
664 #endif
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);
670 #ifdef TE_APOLLO
671     create_target_segments ();
672 #endif
673   }
674
675 #else /* not MANY_SEGMENTS */
676   text_fix_root = NULL;
677   data_fix_root = NULL;
678   bss_fix_root = NULL;
679 #endif /* not MANY_SEGMENTS */
680 #else /* BFD_ASSEMBLER */
681   /* Create the standard sections, and those the assembler uses
682      internally.  */
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);
701
702 #endif /* BFD_ASSEMBLER */
703
704   subseg_set (text_section, 0);
705
706   /* This may add symbol table entries, which requires having an open BFD,
707      and sections already created, in BFD_ASSEMBLER mode.  */
708   md_begin ();
709
710   argv++;                       /* skip argv[0] */
711   argc--;                       /* skip argv[0] */
712   while (argc--)
713     {
714       if (*argv)
715         {                       /* Is it a file-name argument? */
716           saw_a_file++;
717           /* argv->"" if stdin desired, else->filename */
718           read_a_source_file (*argv);
719         }
720       argv++;                   /* completed that argv */
721     }
722   if (!saw_a_file)
723     read_a_source_file ("");
724 }                               /* perform_an_assembly_pass() */
725 \f
726 #if 0
727 /* This is not currently used.  */
728 static SIGTY
729 got_sig (sig)
730      int sig;
731 {
732   static here_before = 0;
733
734   as_bad ("Interrupted by signal %d", sig);
735   if (here_before++)
736     exit (EXIT_FAILURE);
737 #if 0 /* If SIGTY is void, this produces warnings.  */
738   return ((SIGTY) 0);
739 #endif
740 }
741 #endif
742
743 /* end of as.c */