* as.c (show_usage): Remove target specific messages;
[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                      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   md_show_usage (stream);
133 }
134
135 /*
136  * Parse arguments, but we are only interested in flags.
137  * When we find a flag, we process it then make it's argv[] NULL.
138  * This helps any future argv[] scanners avoid what we processed.
139  * Since it is easy to do here we interpret the special arg "-"
140  * to mean "use stdin" and we set that argv[] pointing to "".
141  * After we have munged argv[], the only things left are source file
142  * name(s) and ""(s) denoting stdin. These file names are used
143  * (perhaps more than once) later.
144  *
145  * check for new machine-dep cmdline options in
146  * md_parse_option definitions in config/tc-*.c
147  */
148
149 void
150 parse_args (pargc, pargv)
151      int *pargc;
152      char ***pargv;
153 {
154   int old_argc, new_argc;
155   char **old_argv, **new_argv;
156
157   /* Starting the short option string with '-' is for programs that
158      expect options and other ARGV-elements in any order and that care about
159      the ordering of the two.  We describe each non-option ARGV-element
160      as if it were the argument of an option with character code 1.  */
161
162   char *shortopts;
163   extern CONST char *md_shortopts;
164   CONST char *std_shortopts = "-1JKLRWZfa::DI:o:vwX";
165
166   struct option *longopts;
167   extern struct option md_longopts[];
168   extern size_t md_longopts_size;
169   static struct option std_longopts[] = {
170 #define OPTION_HELP (OPTION_STD_BASE)
171     {"help", no_argument, NULL, OPTION_HELP},
172 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
173     {"nocpp", no_argument, NULL, OPTION_NOCPP},
174 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
175     {"statistics", no_argument, NULL, OPTION_STATISTICS},
176 #define OPTION_VERSION (OPTION_STD_BASE + 3)
177     {"version", no_argument, NULL, OPTION_VERSION},
178   };
179
180   /* Construct the option lists from the standard list and the
181      target dependent list.  */
182   shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
183   longopts = xmalloc (sizeof (std_longopts) + md_longopts_size);
184   memcpy (longopts, std_longopts, sizeof (std_longopts));
185   memcpy ((char *) longopts + sizeof (std_longopts),
186           md_longopts, md_longopts_size);
187
188   /* Make a local copy of the old argv.  */
189   old_argc = *pargc;
190   old_argv = *pargv;
191
192   /* Initialize a new argv that contains no options.  */
193   new_argv = (char **) xmalloc (sizeof (char *) * (old_argc + 1));
194   new_argv[0] = old_argv[0];
195   new_argc = 1;
196   new_argv[new_argc] = NULL;
197
198   while (1)
199     {
200       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
201          indicate a long option.  */
202       int longind;
203       int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
204                                    &longind);
205
206       if (optc == -1)
207         break;
208
209       switch (optc)
210         {
211         default:
212           /* md_parse_option should return 1 if it recognizes optc,
213              0 if not.  */
214           if (md_parse_option (optc, optarg) == 0)
215             exit (EXIT_FAILURE);
216           break;
217
218         case '?':
219           exit (EXIT_FAILURE);
220
221         case 1:                 /* File name.  */
222           if (!strcmp (optarg, "-"))
223             optarg = "";
224           new_argv[new_argc++] = optarg;
225           new_argv[new_argc] = NULL;
226           break;
227
228         case OPTION_HELP:
229           show_usage (stdout);
230           exit (0);
231
232         case OPTION_NOCPP:
233           break;
234
235         case OPTION_STATISTICS:
236           statistics_flag = 1;
237           break;
238
239         case OPTION_VERSION:
240           print_version_id ();
241           exit (0);
242
243         case '1':
244         case 'J':
245         case 'K':
246         case 'L':
247         case 'R':
248         case 'W':
249         case 'Z':
250         case 'f':
251           flagseen[(unsigned char) optc] = 1;
252           break;
253
254         case 'a':
255           if (optarg)
256             {
257               while (*optarg)
258                 {
259                   switch (*optarg)
260                     {
261                     case 'd':
262                       listing |= LISTING_NODEBUG;
263                       break;
264                     case 'h':
265                       listing |= LISTING_HLL;
266                       break;
267                     case 'l':
268                       listing |= LISTING_LISTING;
269                       break;
270                     case 'n':
271                       listing |= LISTING_NOFORM;
272                       break;
273                     case 's':
274                       listing |= LISTING_SYMBOLS;
275                       break;
276                     default:
277                       as_bad ("invalid listing option `%c'", *optarg);
278                       exit (EXIT_FAILURE);
279                       break;
280                     }
281                   optarg++;
282                 }
283             }
284           if (!listing)
285             listing = LISTING_DEFAULT;
286           break;
287
288         case 'D':
289           /* DEBUG is implemented: it debugs different */
290           /* things to other people's assemblers. */
291           break;
292
293         case 'I':
294           {                     /* Include file directory */
295             char *temp = strdup (optarg);
296             if (!temp)
297               as_fatal ("virtual memory exhausted");
298             add_include_dir (temp);
299             break;
300           }
301
302         case 'o':
303           out_file_name = strdup (optarg);
304           if (!out_file_name)
305             as_fatal ("virtual memory exhausted");
306           break;
307
308         case 'v':
309           print_version_id ();
310           break;
311
312         case 'w':
313           break;
314
315         case 'X':
316           /* -X means treat warnings as errors */
317           break;
318         }
319     }
320
321   free (shortopts);
322   free (longopts);
323
324   *pargc = new_argc;
325   *pargv = new_argv;
326 }
327
328 int 
329 main (argc, argv)
330      int argc;
331      char **argv;
332 {
333   char a;
334   int keep_it;
335   long start_time = get_run_time ();
336
337 #if 0 /* do we need any of this?? */
338   {
339     static const int sig[] = {SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
340
341     for (a = 0; sig[a] != 0; a++)
342       if (signal (sig[a], SIG_IGN) != SIG_IGN)
343         signal (sig[a], got_sig);
344   }
345 #endif
346
347   myname = argv[0];
348   memset (flagseen, '\0', sizeof (flagseen));   /* aint seen nothing yet */
349 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
350 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
351 #endif
352   out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
353
354 #ifdef BFD_ASSEMBLER
355   bfd_init ();
356 #endif
357
358   symbol_begin ();
359   subsegs_begin ();
360   read_begin ();
361   input_scrub_begin ();
362   frag_init ();
363   parse_args (&argc, &argv);
364
365 #ifdef BFD_ASSEMBLER
366   output_file_create (out_file_name);
367   assert (stdoutput != 0);
368 #endif
369
370 #ifdef tc_init_after_args
371   tc_init_after_args ();
372 #endif
373
374   /* Here with flags set up in flagseen[]. */
375   perform_an_assembly_pass (argc, argv);        /* Assemble it. */
376 #ifdef TC_I960
377   brtab_emit ();
378 #endif
379
380   if (seen_at_least_1_file ()
381       && !((had_warnings () && flagseen['Z'])
382            || had_errors () > 0))
383     keep_it = 1;
384   else
385     keep_it = 0;
386
387   if (keep_it)
388     write_object_file ();
389
390 #ifndef NO_LISTING
391   listing_print ("");
392 #endif
393
394 #ifndef OBJ_VMS /* does its own file handling */
395 #ifndef BFD_ASSEMBLER
396   if (keep_it)
397 #endif
398     output_file_close (out_file_name);
399 #endif
400
401   if (!keep_it)
402     unlink (out_file_name);
403
404   input_scrub_end ();
405 #ifdef md_end
406   md_end ();
407 #endif
408
409   if (statistics_flag)
410     {
411       extern char **environ;
412       char *lim = (char *) sbrk (0);
413       long run_time = get_run_time () - start_time;
414
415       fprintf (stderr, "%s: total time in assembly: %ld.%06ld\n",
416                myname, run_time / 1000000, run_time % 1000000);
417       fprintf (stderr, "%s: data size %ld\n",
418                myname, (long) (lim - (char *) &environ));
419     }
420
421   if ((had_warnings () && flagseen['Z'])
422       || had_errors () > 0)
423     return EXIT_FAILURE;
424   return EXIT_SUCCESS;
425 }
426 \f
427
428 /*                      perform_an_assembly_pass()
429  *
430  * Here to attempt 1 pass over each input file.
431  * We scan argv[*] looking for filenames or exactly "" which is
432  * shorthand for stdin. Any argv that is NULL is not a file-name.
433  * We set need_pass_2 TRUE if, after this, we still have unresolved
434  * expressions of the form (unknown value)+-(unknown value).
435  *
436  * Note the un*x semantics: there is only 1 logical input file, but it
437  * may be a catenation of many 'physical' input files.
438  */
439 static void 
440 perform_an_assembly_pass (argc, argv)
441      int argc;
442      char **argv;
443 {
444   int saw_a_file = 0;
445 #ifdef BFD_ASSEMBLER
446   flagword applicable;
447 #endif
448
449   need_pass_2 = 0;
450
451 #ifndef BFD_ASSEMBLER
452 #ifdef MANY_SEGMENTS
453   {
454     unsigned int i;
455     for (i = SEG_E0; i < SEG_UNKNOWN; i++)
456       segment_info[i].fix_root = 0;
457   }
458   /* Create the three fixed ones */
459   {
460     segT seg;
461
462 #ifdef TE_APOLLO
463     seg = subseg_new (".wtext", 0);
464 #else
465     seg = subseg_new (".text", 0);
466 #endif
467     assert (seg == SEG_E0);
468     seg = subseg_new (".data", 0);
469     assert (seg == SEG_E1);
470     seg = subseg_new (".bss", 0);
471     assert (seg == SEG_E2);
472 #ifdef TE_APOLLO
473     create_target_segments ();
474 #endif
475   }
476
477 #else /* not MANY_SEGMENTS */
478   text_fix_root = NULL;
479   data_fix_root = NULL;
480   bss_fix_root = NULL;
481 #endif /* not MANY_SEGMENTS */
482 #else /* BFD_ASSEMBLER */
483   /* Create the standard sections, and those the assembler uses
484      internally.  */
485   text_section = subseg_new (".text", 0);
486   data_section = subseg_new (".data", 0);
487   bss_section = subseg_new (".bss", 0);
488   /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
489      to have relocs, otherwise we don't find out in time. */
490   applicable = bfd_applicable_section_flags (stdoutput);
491   bfd_set_section_flags (stdoutput, text_section,
492                          applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
493                                        | SEC_CODE | SEC_READONLY));
494   /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
495   bfd_set_section_flags (stdoutput, data_section,
496                          applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC));
497   bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
498   seg_info (bss_section)->bss = 1;
499   subseg_new (BFD_ABS_SECTION_NAME, 0);
500   subseg_new (BFD_UND_SECTION_NAME, 0);
501   reg_section = subseg_new ("*GAS `reg' section*", 0);
502   expr_section = subseg_new ("*GAS `expr' section*", 0);
503
504 #endif /* BFD_ASSEMBLER */
505
506   subseg_set (text_section, 0);
507
508   /* This may add symbol table entries, which requires having an open BFD,
509      and sections already created, in BFD_ASSEMBLER mode.  */
510   md_begin ();
511
512   argv++;                       /* skip argv[0] */
513   argc--;                       /* skip argv[0] */
514   while (argc--)
515     {
516       if (*argv)
517         {                       /* Is it a file-name argument? */
518           saw_a_file++;
519           /* argv->"" if stdin desired, else->filename */
520           read_a_source_file (*argv);
521         }
522       argv++;                   /* completed that argv */
523     }
524   if (!saw_a_file)
525     read_a_source_file ("");
526 }                               /* perform_an_assembly_pass() */
527 \f
528 #if 0
529 /* This is not currently used.  */
530 static SIGTY
531 got_sig (sig)
532      int sig;
533 {
534   static here_before = 0;
535
536   as_bad ("Interrupted by signal %d", sig);
537   if (here_before++)
538     exit (EXIT_FAILURE);
539 #if 0 /* If SIGTY is void, this produces warnings.  */
540   return ((SIGTY) 0);
541 #endif
542 }
543 #endif
544
545 /* end of as.c */