1 /* as.c - GAS main program.
2    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5 
6    This file is part of GAS, the GNU Assembler.
7 
8    GAS is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12 
13    GAS is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with GAS; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22 
23 /* Main program for AS; a 32-bit assembler of GNU.
24    Understands command arguments.
25    Has a few routines that don't fit in other modules because they
26    are shared.
27 
28   			bugs
29 
30    : initialisers
31   	Since no-one else says they will support them in future: I
32    don't support them now.  */
33 
34 #include "ansidecl.h"
35 
36 #define COMMON
37 
38 #include "as.h"
39 #include "subsegs.h"
40 #include "output-file.h"
41 #include "sb.h"
42 #include "macro.h"
43 #include "dwarf2dbg.h"
44 #include "dw2gencfi.h"
45 
46 #ifdef BFD_ASSEMBLER
47 #include "bfdver.h"
48 #endif
49 
50 #ifdef HAVE_ITBL_CPU
51 #include "itbl-ops.h"
52 #else
53 #define itbl_parse(itbl_file) 1
54 #define itbl_init()
55 #endif
56 
57 #ifdef HAVE_SBRK
58 #ifdef NEED_DECLARATION_SBRK
59 extern PTR sbrk ();
60 #endif
61 #endif
62 
63 #ifdef USING_CGEN
64 /* Perform any cgen specific initialisation for gas.  */
65 extern void gas_cgen_begin (void);
66 #endif
67 
68 /* Keep a record of the itbl files we read in.  */
69 struct itbl_file_list
70 {
71   struct itbl_file_list *next;
72   char *name;
73 };
74 
75 /* We build a list of defsyms as we read the options, and then define
76    them after we have initialized everything.  */
77 struct defsym_list
78 {
79   struct defsym_list *next;
80   char *name;
81   valueT value;
82 };
83 
84 
85 /* True if a listing is wanted.  */
86 int listing;
87 
88 /* Type of debugging to generate.  */
89 enum debug_info_type debug_type = DEBUG_UNSPECIFIED;
90 int use_gnu_debug_info_extensions = 0;
91 
92 #ifndef MD_DEBUG_FORMAT_SELECTOR
93 #define MD_DEBUG_FORMAT_SELECTOR NULL
94 #endif
95 static enum debug_info_type (*md_debug_format_selector) (int *) = MD_DEBUG_FORMAT_SELECTOR;
96 
97 /* Maximum level of macro nesting.  */
98 int max_macro_nest = 100;
99 
100 /* argv[0]  */
101 char * myname;
102 
103 /* The default obstack chunk size.  If we set this to zero, the
104    obstack code will use whatever will fit in a 4096 byte block.  */
105 int chunksize = 0;
106 
107 /* To monitor memory allocation more effectively, make this non-zero.
108    Then the chunk sizes for gas and bfd will be reduced.  */
109 int debug_memory = 0;
110 
111 /* Enable verbose mode.  */
112 int verbose = 0;
113 
114 #ifdef BFD_ASSEMBLER
115 segT reg_section;
116 segT expr_section;
117 segT text_section;
118 segT data_section;
119 segT bss_section;
120 #endif
121 
122 /* Name of listing file.  */
123 static char *listing_filename = NULL;
124 
125 static struct defsym_list *defsyms;
126 
127 static struct itbl_file_list *itbl_files;
128 
129 static long start_time;
130 
131 static int flag_macro_alternate;
132 
133 
134 #ifdef USE_EMULATIONS
135 #define EMULATION_ENVIRON "AS_EMULATION"
136 
137 extern struct emulation mipsbelf, mipslelf, mipself;
138 extern struct emulation mipsbecoff, mipslecoff, mipsecoff;
139 extern struct emulation i386coff, i386elf, i386aout;
140 extern struct emulation crisaout, criself;
141 
142 static struct emulation *const emulations[] = { EMULATIONS };
143 static const int n_emulations = sizeof (emulations) / sizeof (emulations[0]);
144 
145 static void
select_emulation_mode(int argc,char ** argv)146 select_emulation_mode (int argc, char **argv)
147 {
148   int i;
149   char *p, *em = 0;
150 
151   for (i = 1; i < argc; i++)
152     if (!strncmp ("--em", argv[i], 4))
153       break;
154 
155   if (i == argc)
156     goto do_default;
157 
158   p = strchr (argv[i], '=');
159   if (p)
160     p++;
161   else
162     p = argv[i + 1];
163 
164   if (!p || !*p)
165     as_fatal (_("missing emulation mode name"));
166   em = p;
167 
168  do_default:
169   if (em == 0)
170     em = getenv (EMULATION_ENVIRON);
171   if (em == 0)
172     em = DEFAULT_EMULATION;
173 
174   if (em)
175     {
176       for (i = 0; i < n_emulations; i++)
177 	if (!strcmp (emulations[i]->name, em))
178 	  break;
179       if (i == n_emulations)
180 	as_fatal (_("unrecognized emulation name `%s'"), em);
181       this_emulation = emulations[i];
182     }
183   else
184     this_emulation = emulations[0];
185 
186   this_emulation->init ();
187 }
188 
189 const char *
default_emul_bfd_name(void)190 default_emul_bfd_name (void)
191 {
192   abort ();
193   return NULL;
194 }
195 
196 void
common_emul_init(void)197 common_emul_init (void)
198 {
199   this_format = this_emulation->format;
200 
201   if (this_emulation->leading_underscore == 2)
202     this_emulation->leading_underscore = this_format->dfl_leading_underscore;
203 
204   if (this_emulation->default_endian != 2)
205     target_big_endian = this_emulation->default_endian;
206 
207   if (this_emulation->fake_label_name == 0)
208     {
209       if (this_emulation->leading_underscore)
210 	this_emulation->fake_label_name = "L0\001";
211       else
212 	/* What other parameters should we test?  */
213 	this_emulation->fake_label_name = ".L0\001";
214     }
215 }
216 #endif
217 
218 void
print_version_id(void)219 print_version_id (void)
220 {
221   static int printed;
222 
223   if (printed)
224     return;
225   printed = 1;
226 
227 #ifdef BFD_ASSEMBLER
228   fprintf (stderr, _("GNU assembler version %s (%s) using BFD version %s"),
229 	   VERSION, TARGET_ALIAS, BFD_VERSION_STRING);
230 #else
231   fprintf (stderr, _("GNU assembler version %s (%s)"), VERSION, TARGET_ALIAS);
232 #endif
233   fprintf (stderr, "\n");
234 }
235 
236 static void
show_usage(FILE * stream)237 show_usage (FILE * stream)
238 {
239   fprintf (stream, _("Usage: %s [option...] [asmfile...]\n"), myname);
240 
241   fprintf (stream, _("\
242 Options:\n\
243   -a[sub-option...]	  turn on listings\n\
244                       	  Sub-options [default hls]:\n\
245                       	  c      omit false conditionals\n\
246                       	  d      omit debugging directives\n\
247                       	  h      include high-level source\n\
248                       	  l      include assembly\n\
249                       	  m      include macro expansions\n\
250                       	  n      omit forms processing\n\
251                       	  s      include symbols\n\
252                       	  =FILE  list to FILE (must be last sub-option)\n"));
253 
254 /* (TIGCC 20050732) */
255   fprintf (stream, _("\
256   --a68k                  assemble in A68k compatibility mode\n"));
257 
258 /* (TIGCC 20030716) */
259   fprintf (stream, _("\
260   --all-relocs            output all relocs, implies --keep-locals (-L)\n"));
261 
262   fprintf (stream, _("\
263   --alternate             initially turn on alternate macro syntax\n"));
264   fprintf (stream, _("\
265   -D                      produce assembler debugging messages\n"));
266   fprintf (stream, _("\
267   --defsym SYM=VAL        define symbol SYM to given value\n"));
268 #ifdef USE_EMULATIONS
269   {
270     int i;
271     char *def_em;
272 
273     fprintf (stream, "\
274   --em=[");
275     for (i = 0; i < n_emulations - 1; i++)
276       fprintf (stream, "%s | ", emulations[i]->name);
277     fprintf (stream, "%s]\n", emulations[i]->name);
278 
279     def_em = getenv (EMULATION_ENVIRON);
280     if (!def_em)
281       def_em = DEFAULT_EMULATION;
282     fprintf (stream, _("\
283                           emulate output (default %s)\n"), def_em);
284   }
285 #endif
286 #if defined BFD_ASSEMBLER && (defined OBJ_ELF || defined OBJ_MAYBE_ELF)
287   fprintf (stream, _("\
288   --execstack             require executable stack for this object\n"));
289   fprintf (stream, _("\
290   --noexecstack           don't require executable stack for this object\n"));
291 #endif
292   fprintf (stream, _("\
293   -f                      skip whitespace and comment preprocessing\n"));
294   fprintf (stream, _("\
295   -g --gen-debug          generate debugging information\n"));
296   fprintf (stream, _("\
297   --gstabs                generate STABS debugging information\n"));
298   fprintf (stream, _("\
299   --gstabs+               generate STABS debug info with GNU extensions\n"));
300   fprintf (stream, _("\
301   --gdwarf-2              generate DWARF2 debugging information\n"));
302   fprintf (stream, _("\
303   --help                  show this message and exit\n"));
304   fprintf (stream, _("\
305   --target-help           show target specific options\n"));
306   fprintf (stream, _("\
307   -I DIR                  add DIR to search list for .include directives\n"));
308   fprintf (stream, _("\
309   -J                      don't warn about signed overflow\n"));
310   fprintf (stream, _("\
311   -K                      warn when differences altered for long displacements\n"));
312   fprintf (stream, _("\
313   -L,--keep-locals        keep local symbols (e.g. starting with `L')\n"));
314   fprintf (stream, _("\
315   -M,--mri                assemble in MRI compatibility mode\n"));
316   fprintf (stream, _("\
317   --MD FILE               write dependency information in FILE (default none)\n"));
318   fprintf (stream, _("\
319   -nocpp                  ignored\n"));
320   fprintf (stream, _("\
321   -o OBJFILE              name the object-file output OBJFILE (default a.out)\n"));
322   fprintf (stream, _("\
323   -R                      fold data section into text section\n"));
324   fprintf (stream, _("\
325   --statistics            print various measured statistics from execution\n"));
326   fprintf (stream, _("\
327   --strip-local-absolute  strip local absolute symbols\n"));
328   fprintf (stream, _("\
329   --traditional-format    Use same format as native assembler when possible\n"));
330   fprintf (stream, _("\
331   --version               print assembler version number and exit\n"));
332   fprintf (stream, _("\
333   -W  --no-warn           suppress warnings\n"));
334   fprintf (stream, _("\
335   --warn                  don't suppress warnings\n"));
336   fprintf (stream, _("\
337   --fatal-warnings        treat warnings as errors\n"));
338   fprintf (stream, _("\
339   --itbl INSTTBL          extend instruction set to include instructions\n\
340                           matching the specifications defined in file INSTTBL\n"));
341   fprintf (stream, _("\
342   -w                      ignored\n"));
343   fprintf (stream, _("\
344   -X                      ignored\n"));
345   fprintf (stream, _("\
346   -Z                      generate object file even after errors\n"));
347   fprintf (stream, _("\
348   --listing-lhs-width     set the width in words of the output data column of\n\
349                           the listing\n"));
350   fprintf (stream, _("\
351   --listing-lhs-width2    set the width in words of the continuation lines\n\
352                           of the output data column; ignored if smaller than\n\
353                           the width of the first line\n"));
354   fprintf (stream, _("\
355   --listing-rhs-width     set the max width in characters of the lines from\n\
356                           the source file\n"));
357   fprintf (stream, _("\
358   --listing-cont-lines    set the maximum number of continuation lines used\n\
359                           for the output data column of the listing\n"));
360 
361   md_show_usage (stream);
362 
363   fputc ('\n', stream);
364   fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
365 }
366 
367 /* Since it is easy to do here we interpret the special arg "-"
368    to mean "use stdin" and we set that argv[] pointing to "".
369    After we have munged argv[], the only things left are source file
370    name(s) and ""(s) denoting stdin. These file names are used
371    (perhaps more than once) later.
372 
373    check for new machine-dep cmdline options in
374    md_parse_option definitions in config/tc-*.c.  */
375 
376 static void
parse_args(int * pargc,char *** pargv)377 parse_args (int * pargc, char *** pargv)
378 {
379   int old_argc;
380   int new_argc;
381   char ** old_argv;
382   char ** new_argv;
383   /* Starting the short option string with '-' is for programs that
384      expect options and other ARGV-elements in any order and that care about
385      the ordering of the two.  We describe each non-option ARGV-element
386      as if it were the argument of an option with character code 1.  */
387   char *shortopts;
388   extern const char *md_shortopts;
389   static const char std_shortopts[] =
390   {
391     '-', 'J',
392 #ifndef WORKING_DOT_WORD
393     /* -K is not meaningful if .word is not being hacked.  */
394     'K',
395 #endif
396     'L', 'M', 'R', 'W', 'Z', 'a', ':', ':', 'D', 'f', 'g', ':',':', 'I', ':', 'o', ':',
397 #ifndef VMS
398     /* -v takes an argument on VMS, so we don't make it a generic
399        option.  */
400     'v',
401 #endif
402     'w', 'X',
403     /* New option for extending instruction set (see also --itbl below).  */
404     't', ':',
405     '\0'
406   };
407   struct option *longopts;
408   extern struct option md_longopts[];
409   extern size_t md_longopts_size;
410   /* Codes used for the long options with no short synonyms.  */
411   enum option_values
412     {
413       OPTION_HELP = OPTION_STD_BASE,
414       OPTION_NOCPP,
415       OPTION_STATISTICS,
416       OPTION_VERSION,
417       OPTION_DUMPCONFIG,
418       OPTION_VERBOSE,
419       OPTION_EMULATION,
420       OPTION_DEFSYM,
421       OPTION_INSTTBL,
422       OPTION_LISTING_LHS_WIDTH,
423       OPTION_LISTING_LHS_WIDTH2,
424       OPTION_LISTING_RHS_WIDTH,
425       OPTION_LISTING_CONT_LINES,
426       OPTION_DEPFILE,
427       OPTION_GSTABS,
428       OPTION_GSTABS_PLUS,
429       OPTION_GDWARF2,
430       OPTION_STRIP_LOCAL_ABSOLUTE,
431       OPTION_TRADITIONAL_FORMAT,
432       OPTION_WARN,
433       OPTION_TARGET_HELP,
434       OPTION_EXECSTACK,
435       OPTION_NOEXECSTACK,
436       OPTION_ALTERNATE,
437       OPTION_WARN_FATAL,
438       OPTION_ALL_RELOCS, /* (TIGCC 20030716) */
439       OPTION_A68K /* (TIGCC 20050731) */
440     /* When you add options here, check that they do
441        not collide with OPTION_MD_BASE.  See as.h.  */
442     };
443 
444   static const struct option std_longopts[] =
445   {
446     /* Note: commas are placed at the start of the line rather than
447        the end of the preceeding line so that it is simpler to
448        selectively add and remove lines from this list.  */
449     {"alternate", no_argument, NULL, OPTION_ALTERNATE}
450     /* The entry for "a" is here to prevent getopt_long_only() from
451        considering that -a is an abbreviation for --alternate.  This is
452        necessary because -a=<FILE> is a valid switch but getopt would
453        normally reject it since --alternate does not take an argument.  */
454     ,{"a", optional_argument, NULL, 'a'}
455 /* (TIGCC 20050731) */
456     ,{"a68k", no_argument, NULL, OPTION_A68K}
457 /* (TIGCC 20030716) */
458     ,{"all-relocs", no_argument, NULL, OPTION_ALL_RELOCS}
459     ,{"defsym", required_argument, NULL, OPTION_DEFSYM}
460     ,{"dump-config", no_argument, NULL, OPTION_DUMPCONFIG}
461     ,{"emulation", required_argument, NULL, OPTION_EMULATION}
462 #if defined BFD_ASSEMBLER && (defined OBJ_ELF || defined OBJ_MAYBE_ELF)
463     ,{"execstack", no_argument, NULL, OPTION_EXECSTACK}
464     ,{"noexecstack", no_argument, NULL, OPTION_NOEXECSTACK}
465 #endif
466     ,{"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL}
467     ,{"gdwarf-2", no_argument, NULL, OPTION_GDWARF2}
468     /* GCC uses --gdwarf-2 but GAS uses to use --gdwarf2,
469        so we keep it here for backwards compatibility.  */
470     ,{"gdwarf2", no_argument, NULL, OPTION_GDWARF2}
471     ,{"gen-debug", no_argument, NULL, 'g'}
472     ,{"gstabs", no_argument, NULL, OPTION_GSTABS}
473     ,{"gstabs+", no_argument, NULL, OPTION_GSTABS_PLUS}
474     ,{"help", no_argument, NULL, OPTION_HELP}
475     /* New option for extending instruction set (see also -t above).
476        The "-t file" or "--itbl file" option extends the basic set of
477        valid instructions by reading "file", a text file containing a
478        list of instruction formats.  The additional opcodes and their
479        formats are added to the built-in set of instructions, and
480        mnemonics for new registers may also be defined.  */
481     ,{"itbl", required_argument, NULL, OPTION_INSTTBL}
482     /* getopt allows abbreviations, so we do this to stop it from
483        treating -k as an abbreviation for --keep-locals.  Some
484        ports use -k to enable PIC assembly.  */
485     ,{"keep-locals", no_argument, NULL, 'L'}
486     ,{"keep-locals", no_argument, NULL, 'L'}
487     ,{"listing-lhs-width", required_argument, NULL, OPTION_LISTING_LHS_WIDTH}
488     ,{"listing-lhs-width2", required_argument, NULL, OPTION_LISTING_LHS_WIDTH2}
489     ,{"listing-rhs-width", required_argument, NULL, OPTION_LISTING_RHS_WIDTH}
490     ,{"listing-cont-lines", required_argument, NULL, OPTION_LISTING_CONT_LINES}
491     ,{"MD", required_argument, NULL, OPTION_DEPFILE}
492     ,{"mri", no_argument, NULL, 'M'}
493     ,{"nocpp", no_argument, NULL, OPTION_NOCPP}
494     ,{"no-warn", no_argument, NULL, 'W'}
495     ,{"statistics", no_argument, NULL, OPTION_STATISTICS}
496     ,{"strip-local-absolute", no_argument, NULL, OPTION_STRIP_LOCAL_ABSOLUTE}
497     ,{"version", no_argument, NULL, OPTION_VERSION}
498     ,{"verbose", no_argument, NULL, OPTION_VERBOSE}
499     ,{"target-help", no_argument, NULL, OPTION_TARGET_HELP}
500     ,{"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT}
501     ,{"warn", no_argument, NULL, OPTION_WARN}
502   };
503 
504   /* Construct the option lists from the standard list and the target
505      dependent list.  Include space for an extra NULL option and
506      always NULL terminate.  */
507   shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
508   longopts = xmalloc (sizeof (std_longopts) + md_longopts_size + sizeof (struct option));
509   memcpy (longopts, std_longopts, sizeof (std_longopts));
510   memcpy (((char *) longopts) + sizeof (std_longopts), md_longopts, md_longopts_size);
511   memset (((char *) longopts) + sizeof (std_longopts) + md_longopts_size,
512 	  0, sizeof (struct option));
513 
514   /* Make a local copy of the old argv.  */
515   old_argc = *pargc;
516   old_argv = *pargv;
517 
518   /* Initialize a new argv that contains no options.  */
519   new_argv = xmalloc (sizeof (char *) * (old_argc + 1));
520   new_argv[0] = old_argv[0];
521   new_argc = 1;
522   new_argv[new_argc] = NULL;
523 
524   while (1)
525     {
526       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
527 	 indicate a long option.  */
528       int longind;
529       int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
530 				   &longind);
531 
532       if (optc == -1)
533 	break;
534 
535       switch (optc)
536 	{
537 	default:
538 	  /* md_parse_option should return 1 if it recognizes optc,
539 	     0 if not.  */
540 	  if (md_parse_option (optc, optarg) != 0)
541 	    break;
542 	  /* `-v' isn't included in the general short_opts list, so check for
543 	     it explicitly here before deciding we've gotten a bad argument.  */
544 	  if (optc == 'v')
545 	    {
546 #ifdef VMS
547 	      /* Telling getopt to treat -v's value as optional can result
548 		 in it picking up a following filename argument here.  The
549 		 VMS code in md_parse_option can return 0 in that case,
550 		 but it has no way of pushing the filename argument back.  */
551 	      if (optarg && *optarg)
552 		new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
553 	      else
554 #else
555 	      case 'v':
556 #endif
557 	      case OPTION_VERBOSE:
558 		print_version_id ();
559 		verbose = 1;
560 	      break;
561 	    }
562 	  else
563 	    as_bad (_("unrecognized option -%c%s"), optc, optarg ? optarg : "");
564 	  /* Fall through.  */
565 
566 	case '?':
567 	  exit (EXIT_FAILURE);
568 
569 	case 1:			/* File name.  */
570 	  if (!strcmp (optarg, "-"))
571 	    optarg = "";
572 	  new_argv[new_argc++] = optarg;
573 	  new_argv[new_argc] = NULL;
574 	  break;
575 
576 	case OPTION_TARGET_HELP:
577 	  md_show_usage (stdout);
578 	  exit (EXIT_SUCCESS);
579 
580 	case OPTION_HELP:
581 	  show_usage (stdout);
582 	  exit (EXIT_SUCCESS);
583 
584 	case OPTION_NOCPP:
585 	  break;
586 
587 	case OPTION_STATISTICS:
588 	  flag_print_statistics = 1;
589 	  break;
590 
591 	case OPTION_STRIP_LOCAL_ABSOLUTE:
592 	  flag_strip_local_absolute = 1;
593 	  break;
594 
595 	case OPTION_TRADITIONAL_FORMAT:
596 	  flag_traditional_format = 1;
597 	  break;
598 
599 	case OPTION_VERSION:
600 	  /* This output is intended to follow the GNU standards document.  */
601 #ifdef BFD_ASSEMBLER
602 	  printf (_("GNU assembler %s\n"), BFD_VERSION_STRING);
603 #else
604 	  printf (_("GNU assembler %s\n"), VERSION);
605 #endif
606 	  printf (_("Copyright 2005 Free Software Foundation, Inc.\n"));
607 	  printf (_("\
608 This program is free software; you may redistribute it under the terms of\n\
609 the GNU General Public License.  This program has absolutely no warranty.\n"));
610 	  printf (_("This assembler was configured for a target of `%s'.\n"),
611 		  TARGET_ALIAS);
612 	  exit (EXIT_SUCCESS);
613 
614 	case OPTION_EMULATION:
615 #ifdef USE_EMULATIONS
616 	  if (strcmp (optarg, this_emulation->name))
617 	    as_fatal (_("multiple emulation names specified"));
618 #else
619 	  as_fatal (_("emulations not handled in this configuration"));
620 #endif
621 	  break;
622 
623 	case OPTION_DUMPCONFIG:
624 	  fprintf (stderr, _("alias = %s\n"), TARGET_ALIAS);
625 	  fprintf (stderr, _("canonical = %s\n"), TARGET_CANONICAL);
626 	  fprintf (stderr, _("cpu-type = %s\n"), TARGET_CPU);
627 #ifdef TARGET_OBJ_FORMAT
628 	  fprintf (stderr, _("format = %s\n"), TARGET_OBJ_FORMAT);
629 #endif
630 #ifdef TARGET_FORMAT
631 	  fprintf (stderr, _("bfd-target = %s\n"), TARGET_FORMAT);
632 #endif
633 	  exit (EXIT_SUCCESS);
634 
635 	case OPTION_DEFSYM:
636 	  {
637 	    char *s;
638 	    valueT i;
639 	    struct defsym_list *n;
640 
641 	    for (s = optarg; *s != '\0' && *s != '='; s++)
642 	      ;
643 	    if (*s == '\0')
644 	      as_fatal (_("bad defsym; format is --defsym name=value"));
645 	    *s++ = '\0';
646 #ifdef BFD_ASSEMBLER
647 	    i = bfd_scan_vma (s, (const char **) NULL, 0);
648 #else
649 	    i = strtol (s, (char **) NULL, 0);
650 #endif
651 	    n = xmalloc (sizeof *n);
652 	    n->next = defsyms;
653 	    n->name = optarg;
654 	    n->value = i;
655 	    defsyms = n;
656 	  }
657 	  break;
658 
659 	case OPTION_INSTTBL:
660 	case 't':
661 	  {
662 	    /* optarg is the name of the file containing the instruction
663 	       formats, opcodes, register names, etc.  */
664 	    struct itbl_file_list *n;
665 
666 	    if (optarg == NULL)
667 	      {
668 		as_warn (_("no file name following -t option"));
669 		break;
670 	      }
671 
672 	    n = xmalloc (sizeof * n);
673 	    n->next = itbl_files;
674 	    n->name = optarg;
675 	    itbl_files = n;
676 
677 	    /* Parse the file and add the new instructions to our internal
678 	       table.  If multiple instruction tables are specified, the
679 	       information from this table gets appended onto the existing
680 	       internal table.  */
681 	    itbl_files->name = xstrdup (optarg);
682 	    if (itbl_parse (itbl_files->name) != 0)
683 	      as_fatal (_("failed to read instruction table %s\n"),
684 			itbl_files->name);
685 	  }
686 	  break;
687 
688 	case OPTION_DEPFILE:
689 	  start_dependencies (optarg);
690 	  break;
691 
692 	case 'g':
693 	  /* Some backends, eg Alpha and Mips, use the -g switch for their
694 	     own purposes.  So we check here for an explicit -g and allow
695 	     the backend to decide if it wants to process it.  */
696 	  if (   old_argv[optind - 1][1] == 'g'
697 	      && md_parse_option (optc, optarg))
698 	    continue;
699 
700 	  if (md_debug_format_selector)
701 	    debug_type = md_debug_format_selector (& use_gnu_debug_info_extensions);
702 	  else if (IS_ELF)
703 	    debug_type = DEBUG_DWARF2;
704 	  else
705 	    debug_type = DEBUG_STABS;
706 	  break;
707 
708 	case OPTION_GSTABS_PLUS:
709 	  use_gnu_debug_info_extensions = 1;
710 	  /* Fall through.  */
711 	case OPTION_GSTABS:
712 	  debug_type = DEBUG_STABS;
713 	  break;
714 
715 	case OPTION_GDWARF2:
716 	  debug_type = DEBUG_DWARF2;
717 	  break;
718 
719 	case 'J':
720 	  flag_signed_overflow_ok = 1;
721 	  break;
722 
723 #ifndef WORKING_DOT_WORD
724 	case 'K':
725 	  flag_warn_displacement = 1;
726 	  break;
727 #endif
728 	case 'L':
729 	  flag_keep_locals = 1;
730 	  break;
731 
732 	case OPTION_LISTING_LHS_WIDTH:
733 	  listing_lhs_width = atoi (optarg);
734 	  if (listing_lhs_width_second < listing_lhs_width)
735 	    listing_lhs_width_second = listing_lhs_width;
736 	  break;
737 	case OPTION_LISTING_LHS_WIDTH2:
738 	  {
739 	    int tmp = atoi (optarg);
740 
741 	    if (tmp > listing_lhs_width)
742 	      listing_lhs_width_second = tmp;
743 	  }
744 	  break;
745 	case OPTION_LISTING_RHS_WIDTH:
746 	  listing_rhs_width = atoi (optarg);
747 	  break;
748 	case OPTION_LISTING_CONT_LINES:
749 	  listing_lhs_cont_lines = atoi (optarg);
750 	  break;
751 
752 	case OPTION_A68K: /* (TIGCC 20050731) */
753 	  flag_a68k = 1;
754 	  /* fall through */
755 	case 'M':
756 	  flag_mri = 1;
757 #ifdef TC_M68K
758 	  flag_m68k_mri = 1;
759 #endif
760 	  break;
761 
762 	case 'R':
763 	  flag_readonly_data_in_text = 1;
764 	  break;
765 
766 	case 'W':
767 	  flag_no_warnings = 1;
768 	  break;
769 
770 	case OPTION_WARN:
771 	  flag_no_warnings = 0;
772 	  flag_fatal_warnings = 0;
773 	  break;
774 
775 	case OPTION_WARN_FATAL:
776 	  flag_no_warnings = 0;
777 	  flag_fatal_warnings = 1;
778 	  break;
779 
780 #if defined BFD_ASSEMBLER && (defined OBJ_ELF || defined OBJ_MAYBE_ELF)
781 	case OPTION_EXECSTACK:
782 	  flag_execstack = 1;
783 	  flag_noexecstack = 0;
784 	  break;
785 
786 	case OPTION_NOEXECSTACK:
787 	  flag_noexecstack = 1;
788 	  flag_execstack = 0;
789 	  break;
790 #endif
791 	case 'Z':
792 	  flag_always_generate_output = 1;
793 	  break;
794 
795  	case OPTION_ALTERNATE:
796  	  optarg = old_argv [optind - 1];
797  	  while (* optarg == '-')
798  	    optarg ++;
799 
800  	  if (strcmp (optarg, "alternate") == 0)
801  	    {
802  	      flag_macro_alternate = 1;
803  	      break;
804  	    }
805  	  optarg ++;
806  	  /* Fall through.  */
807 
808 	case 'a':
809 	  if (optarg)
810 	    {
811 	      if (optarg != old_argv[optind] && optarg[-1] == '=')
812 		--optarg;
813 
814 	      if (md_parse_option (optc, optarg) != 0)
815 		break;
816 
817 	      while (*optarg)
818 		{
819 		  switch (*optarg)
820 		    {
821 		    case 'c':
822 		      listing |= LISTING_NOCOND;
823 		      break;
824 		    case 'd':
825 		      listing |= LISTING_NODEBUG;
826 		      break;
827 		    case 'h':
828 		      listing |= LISTING_HLL;
829 		      break;
830 		    case 'l':
831 		      listing |= LISTING_LISTING;
832 		      break;
833 		    case 'm':
834 		      listing |= LISTING_MACEXP;
835 		      break;
836 		    case 'n':
837 		      listing |= LISTING_NOFORM;
838 		      break;
839 		    case 's':
840 		      listing |= LISTING_SYMBOLS;
841 		      break;
842 		    case '=':
843 		      listing_filename = xstrdup (optarg + 1);
844 		      optarg += strlen (listing_filename);
845 		      break;
846 		    default:
847 		      as_fatal (_("invalid listing option `%c'"), *optarg);
848 		      break;
849 		    }
850 		  optarg++;
851 		}
852 	    }
853 	  if (!listing)
854 	    listing = LISTING_DEFAULT;
855 	  break;
856 
857 	case 'D':
858 	  /* DEBUG is implemented: it debugs different
859 	     things from other people's assemblers.  */
860 	  flag_debug = 1;
861 	  break;
862 
863 	case 'f':
864 	  flag_no_comments = 1;
865 	  break;
866 
867 	case 'I':
868 	  {			/* Include file directory.  */
869 	    char *temp = xstrdup (optarg);
870 
871 	    add_include_dir (temp);
872 	    break;
873 	  }
874 
875 	case 'o':
876 	  out_file_name = xstrdup (optarg);
877 	  break;
878 
879 	case 'w':
880 	  break;
881 
882 	case 'X':
883 	  /* -X means treat warnings as errors.  */
884 	  break;
885 
886 	/* (TIGCC 20030716) */
887 	case OPTION_ALL_RELOCS:
888 	  flag_all_relocs = 1;
889 	  flag_keep_locals = 1; /* implies -L */
890 	  break;
891 	}
892     }
893 
894   free (shortopts);
895   free (longopts);
896 
897   *pargc = new_argc;
898   *pargv = new_argv;
899 
900 #ifdef md_after_parse_args
901   md_after_parse_args ();
902 #endif
903 }
904 
905 static void
dump_statistics(void)906 dump_statistics (void)
907 {
908 #ifdef HAVE_SBRK
909   char *lim = (char *) sbrk (0);
910 #endif
911   long run_time = get_run_time () - start_time;
912 
913   fprintf (stderr, _("%s: total time in assembly: %ld.%06ld\n"),
914 	   myname, run_time / 1000000, run_time % 1000000);
915 #ifdef HAVE_SBRK
916   fprintf (stderr, _("%s: data size %ld\n"),
917 	   myname, (long) (lim - (char *) &environ));
918 #endif
919 
920   subsegs_print_statistics (stderr);
921   write_print_statistics (stderr);
922   symbol_print_statistics (stderr);
923   read_print_statistics (stderr);
924 
925 #ifdef tc_print_statistics
926   tc_print_statistics (stderr);
927 #endif
928 
929 #ifdef obj_print_statistics
930   obj_print_statistics (stderr);
931 #endif
932 }
933 
934 /* The interface between the macro code and gas expression handling.  */
935 
936 static int
macro_expr(const char * emsg,int idx,sb * in,int * val)937 macro_expr (const char *emsg, int idx, sb *in, int *val)
938 {
939   char *hold;
940   expressionS ex;
941 
942   sb_terminate (in);
943 
944   hold = input_line_pointer;
945   input_line_pointer = in->ptr + idx;
946   expression (&ex);
947   idx = input_line_pointer - in->ptr;
948   input_line_pointer = hold;
949 
950   if (ex.X_op != O_constant)
951     as_bad ("%s", emsg);
952 
953   *val = (int) ex.X_add_number;
954 
955   return idx;
956 }
957 
958 /* Here to attempt 1 pass over each input file.
959    We scan argv[*] looking for filenames or exactly "" which is
960    shorthand for stdin. Any argv that is NULL is not a file-name.
961    We set need_pass_2 TRUE if, after this, we still have unresolved
962    expressions of the form (unknown value)+-(unknown value).
963 
964    Note the un*x semantics: there is only 1 logical input file, but it
965    may be a catenation of many 'physical' input files.  */
966 
967 static void
perform_an_assembly_pass(int argc,char ** argv)968 perform_an_assembly_pass (int argc, char ** argv)
969 {
970   int saw_a_file = 0;
971 #ifdef BFD_ASSEMBLER
972   flagword applicable;
973 #endif
974 
975   need_pass_2 = 0;
976 
977 #ifndef BFD_ASSEMBLER
978 #ifdef MANY_SEGMENTS
979   {
980     unsigned int i;
981     for (i = SEG_E0; i < SEG_UNKNOWN; i++)
982       segment_info[i].fix_root = 0;
983   }
984   /* Create the three fixed ones.  */
985   {
986     segT seg;
987 
988 #ifdef TE_APOLLO
989     seg = subseg_new (".wtext", 0);
990 #else
991     seg = subseg_new (".text", 0);
992 #endif
993     assert (seg == SEG_E0);
994     seg = subseg_new (".data", 0);
995     assert (seg == SEG_E1);
996     seg = subseg_new (".bss", 0);
997     assert (seg == SEG_E2);
998 #ifdef TE_APOLLO
999     create_target_segments ();
1000 #endif
1001   }
1002 
1003 #else /* not MANY_SEGMENTS.  */
1004   text_fix_root = NULL;
1005   data_fix_root = NULL;
1006   bss_fix_root = NULL;
1007 #endif /* not MANY_SEGMENTS.  */
1008 #else /* BFD_ASSEMBLER.  */
1009   /* Create the standard sections, and those the assembler uses
1010      internally.  */
1011   text_section = subseg_new (TEXT_SECTION_NAME, 0);
1012   data_section = subseg_new (DATA_SECTION_NAME, 0);
1013   bss_section = subseg_new (BSS_SECTION_NAME, 0);
1014   /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
1015      to have relocs, otherwise we don't find out in time.  */
1016   applicable = bfd_applicable_section_flags (stdoutput);
1017   bfd_set_section_flags (stdoutput, text_section,
1018 			 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1019 				       | SEC_CODE | SEC_READONLY));
1020   bfd_set_section_flags (stdoutput, data_section,
1021 			 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1022 				       | SEC_DATA));
1023   bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
1024   seg_info (bss_section)->bss = 1;
1025   subseg_new (BFD_ABS_SECTION_NAME, 0);
1026   subseg_new (BFD_UND_SECTION_NAME, 0);
1027   reg_section = subseg_new ("*GAS `reg' section*", 0);
1028   expr_section = subseg_new ("*GAS `expr' section*", 0);
1029 
1030 #endif /* BFD_ASSEMBLER.  */
1031 
1032   subseg_set (text_section, 0);
1033 
1034   /* This may add symbol table entries, which requires having an open BFD,
1035      and sections already created, in BFD_ASSEMBLER mode.  */
1036   md_begin ();
1037 
1038 #ifdef USING_CGEN
1039   gas_cgen_begin ();
1040 #endif
1041 #ifdef obj_begin
1042   obj_begin ();
1043 #endif
1044 
1045   /* Skip argv[0].  */
1046   argv++;
1047   argc--;
1048 
1049   while (argc--)
1050     {
1051       if (*argv)
1052 	{			/* Is it a file-name argument?  */
1053 	  PROGRESS (1);
1054 	  saw_a_file++;
1055 	  /* argv->"" if stdin desired, else->filename.  */
1056 	  read_a_source_file (*argv);
1057 	}
1058       argv++;			/* Completed that argv.  */
1059     }
1060   if (!saw_a_file)
1061     read_a_source_file ("");
1062 }
1063 
1064 
1065 int
main(int argc,char ** argv)1066 main (int argc, char ** argv)
1067 {
1068   int macro_strip_at;
1069   int keep_it;
1070 
1071   start_time = get_run_time ();
1072 
1073 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1074   setlocale (LC_MESSAGES, "");
1075 #endif
1076 #if defined (HAVE_SETLOCALE)
1077   setlocale (LC_CTYPE, "");
1078 #endif
1079   bindtextdomain (PACKAGE, LOCALEDIR);
1080   textdomain (PACKAGE);
1081 
1082   if (debug_memory)
1083     chunksize = 64;
1084 
1085 #ifdef HOST_SPECIAL_INIT
1086   HOST_SPECIAL_INIT (argc, argv);
1087 #endif
1088 
1089   myname = argv[0];
1090   xmalloc_set_program_name (myname);
1091 
1092   START_PROGRESS (myname, 0);
1093 
1094 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
1095 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
1096 #endif
1097 
1098   out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
1099 
1100   hex_init ();
1101 #ifdef BFD_ASSEMBLER
1102   bfd_init ();
1103   bfd_set_error_program_name (myname);
1104 #endif
1105 
1106 #ifdef USE_EMULATIONS
1107   select_emulation_mode (argc, argv);
1108 #endif
1109 
1110   PROGRESS (1);
1111   symbol_begin ();
1112   frag_init ();
1113   subsegs_begin ();
1114   parse_args (&argc, &argv);
1115   read_begin ();
1116   input_scrub_begin ();
1117   expr_begin ();
1118 
1119   if (flag_print_statistics)
1120     xatexit (dump_statistics);
1121 
1122   macro_strip_at = 0;
1123 #ifdef TC_I960
1124   macro_strip_at = flag_mri;
1125 #endif
1126 #ifdef TC_A29K
1127   /* For compatibility with the AMD 29K family macro assembler
1128      specification.  */
1129   flag_macro_alternate = 1;
1130   macro_strip_at = 1;
1131 #endif
1132 
1133   macro_init (flag_macro_alternate, flag_mri, macro_strip_at, macro_expr);
1134 
1135   PROGRESS (1);
1136 
1137 #ifdef BFD_ASSEMBLER
1138   output_file_create (out_file_name);
1139   assert (stdoutput != 0);
1140 #endif
1141 
1142 #ifdef tc_init_after_args
1143   tc_init_after_args ();
1144 #endif
1145 
1146   itbl_init ();
1147 
1148   /* Now that we have fully initialized, and have created the output
1149      file, define any symbols requested by --defsym command line
1150      arguments.  */
1151   while (defsyms != NULL)
1152     {
1153       symbolS *sym;
1154       struct defsym_list *next;
1155 
1156       sym = symbol_new (defsyms->name, absolute_section, defsyms->value,
1157 			&zero_address_frag);
1158       symbol_table_insert (sym);
1159       next = defsyms->next;
1160       free (defsyms);
1161       defsyms = next;
1162     }
1163 
1164   PROGRESS (1);
1165 
1166   /* Assemble it.  */
1167   perform_an_assembly_pass (argc, argv);
1168 
1169   cond_finish_check (-1);
1170 
1171 #ifdef md_end
1172   md_end ();
1173 #endif
1174 
1175 #if defined BFD_ASSEMBLER && (defined OBJ_ELF || defined OBJ_MAYBE_ELF)
1176   if ((flag_execstack || flag_noexecstack)
1177       && OUTPUT_FLAVOR == bfd_target_elf_flavour)
1178     {
1179       segT gnustack;
1180 
1181       gnustack = subseg_new (".note.GNU-stack", 0);
1182       bfd_set_section_flags (stdoutput, gnustack,
1183 			     SEC_READONLY | (flag_execstack ? SEC_CODE : 0));
1184 
1185     }
1186 #endif
1187 
1188   /* If we've been collecting dwarf2 .debug_line info, either for
1189      assembly debugging or on behalf of the compiler, emit it now.  */
1190   dwarf2_finish ();
1191 
1192   /* If we constructed dwarf2 .eh_frame info, either via .cfi
1193      directives from the user or by the backend, emit it now.  */
1194   cfi_finish ();
1195 
1196   if (seen_at_least_1_file ()
1197       && (flag_always_generate_output || had_errors () == 0))
1198     keep_it = 1;
1199   else
1200     keep_it = 0;
1201 
1202 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1203   /* This used to be done at the start of write_object_file in
1204      write.c, but that caused problems when doing listings when
1205      keep_it was zero.  This could probably be moved above md_end, but
1206      I didn't want to risk the change.  */
1207   subsegs_finish ();
1208 #endif
1209 
1210   if (keep_it)
1211     write_object_file ();
1212 
1213 #ifndef NO_LISTING
1214   listing_print (listing_filename);
1215 #endif
1216 
1217 #ifndef OBJ_VMS /* Does its own file handling.  */
1218 #ifndef BFD_ASSEMBLER
1219   if (keep_it)
1220 #endif
1221     output_file_close (out_file_name);
1222 #endif
1223 
1224   if (flag_fatal_warnings && had_warnings () > 0 && had_errors () == 0)
1225     as_bad (_("%d warnings, treating warnings as errors"), had_warnings ());
1226 
1227   if (had_errors () > 0 && ! flag_always_generate_output)
1228     keep_it = 0;
1229 
1230   if (!keep_it)
1231     unlink_if_ordinary (out_file_name);
1232 
1233   input_scrub_end ();
1234 
1235   END_PROGRESS (myname);
1236 
1237   /* Use xexit instead of return, because under VMS environments they
1238      may not place the same interpretation on the value given.  */
1239   if (had_errors () > 0)
1240     xexit (EXIT_FAILURE);
1241 
1242   /* Only generate dependency file if assembler was successful.  */
1243   print_dependencies ();
1244 
1245   xexit (EXIT_SUCCESS);
1246 }
1247