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