as.c revision 77298
120253Sjoerg/* as.c - GAS main program.
220302Sjoerg   Copyright (C) 1987, 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001
320302Sjoerg   Free Software Foundation, Inc.
420253Sjoerg
520253Sjoerg   This file is part of GAS, the GNU Assembler.
620253Sjoerg
720253Sjoerg   GAS is free software; you can redistribute it and/or modify
820253Sjoerg   it under the terms of the GNU General Public License as published by
920302Sjoerg   the Free Software Foundation; either version 2, or (at your option)
1020253Sjoerg   any later version.
1120253Sjoerg
1220253Sjoerg   GAS is distributed in the hope that it will be useful,
1320253Sjoerg   but WITHOUT ANY WARRANTY; without even the implied warranty of
1420302Sjoerg   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1520253Sjoerg   GNU General Public License for more details.
1620253Sjoerg
1720302Sjoerg   You should have received a copy of the GNU General Public License
1820253Sjoerg   along with GAS; see the file COPYING.  If not, write to the Free
1920253Sjoerg   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2020253Sjoerg   02111-1307, USA.  */
2120253Sjoerg
2220253Sjoerg/* Main program for AS; a 32-bit assembler of GNU.
2320253Sjoerg * Understands command arguments.
2420253Sjoerg * Has a few routines that don't fit in other modules because they
2520253Sjoerg * are shared.
2620747Sdavidn *
2720253Sjoerg *			bugs
2820253Sjoerg *
2920253Sjoerg * : initialisers
3020253Sjoerg *	Since no-one else says they will support them in future: I
3120253Sjoerg * don't support them now.
3220253Sjoerg */
3320253Sjoerg
3420747Sdavidn#include "ansidecl.h"
3520253Sjoerg
3620253Sjoerg#define COMMON
3720253Sjoerg
3820253Sjoerg#include "as.h"
3920253Sjoerg#include "subsegs.h"
4020253Sjoerg#include "output-file.h"
4120253Sjoerg#include "sb.h"
4220253Sjoerg#include "macro.h"
4320253Sjoerg#include "dwarf2dbg.h"
4420253Sjoerg
4520253Sjoerg#ifdef HAVE_ITBL_CPU
4620253Sjoerg#include "itbl-ops.h"
4720253Sjoerg#else
4820253Sjoerg#define itbl_parse(itbl_file) 1
4920253Sjoerg#define itbl_init()
5020253Sjoerg#endif
5120253Sjoerg
5220253Sjoerg#ifdef HAVE_SBRK
5320253Sjoerg#ifdef NEED_DECLARATION_SBRK
5420253Sjoergextern PTR sbrk ();
5520253Sjoerg#endif
5620253Sjoerg#endif
5720253Sjoerg
5820253Sjoergstatic void show_usage PARAMS ((FILE *));
5920253Sjoergstatic void parse_args PARAMS ((int *, char ***));
6020253Sjoergstatic void dump_statistics PARAMS ((void));
6120253Sjoergstatic void perform_an_assembly_pass PARAMS ((int argc, char **argv));
6220253Sjoergstatic int macro_expr PARAMS ((const char *, int, sb *, int *));
6320253Sjoerg
6420253Sjoerg/* True if a listing is wanted.  */
6520253Sjoergint listing;
6620253Sjoerg
6720253Sjoerg/* Name of listing file.  */
6820253Sjoergstatic char *listing_filename = NULL;
6920253Sjoerg
7020253Sjoerg/* Type of debugging to generate.  */
7120253Sjoerg
7220253Sjoergenum debug_info_type debug_type = DEBUG_UNSPECIFIED;
7320253Sjoerg
7420253Sjoerg/* Maximum level of macro nesting.  */
7520253Sjoergint max_macro_nest = 100;
7620253Sjoerg
7720253Sjoerg/* argv[0]  */
7820253Sjoergchar *myname;
7920253Sjoerg#ifdef BFD_ASSEMBLER
8020253SjoergsegT reg_section, expr_section;
8120253SjoergsegT text_section, data_section, bss_section;
8220253Sjoerg#endif
8320253Sjoerg
8420253Sjoerg/* The default obstack chunk size.  If we set this to zero, the
8520253Sjoerg   obstack code will use whatever will fit in a 4096 byte block.  */
8620253Sjoergint chunksize = 0;
8720253Sjoerg
8820253Sjoerg/* To monitor memory allocation more effectively, make this non-zero.
8920253Sjoerg   Then the chunk sizes for gas and bfd will be reduced.  */
9020253Sjoergint debug_memory = 0;
9120253Sjoerg
9220747Sdavidn/* We build a list of defsyms as we read the options, and then define
9320253Sjoerg   them after we have initialized everything.  */
9420253Sjoerg
9520253Sjoergstruct defsym_list {
9620253Sjoerg  struct defsym_list *next;
9720253Sjoerg  char *name;
9820253Sjoerg  valueT value;
9920253Sjoerg};
10020253Sjoerg
10120253Sjoergstatic struct defsym_list *defsyms;
10220253Sjoerg
10320253Sjoerg/* Keep a record of the itbl files we read in.  */
10420253Sjoerg
10520253Sjoergstruct itbl_file_list {
10620253Sjoerg  struct itbl_file_list *next;
10720253Sjoerg  char *name;
10820253Sjoerg};
10920253Sjoerg
11020253Sjoergstatic struct itbl_file_list *itbl_files;
11120253Sjoerg
11220253Sjoerg#ifdef USE_EMULATIONS
11320253Sjoerg#define EMULATION_ENVIRON "AS_EMULATION"
11420253Sjoerg
11520253Sjoergextern struct emulation mipsbelf, mipslelf, mipself;
11620253Sjoergextern struct emulation mipsbecoff, mipslecoff, mipsecoff;
11720253Sjoergextern struct emulation i386coff, i386elf, i386aout;
11820253Sjoergextern struct emulation crisaout, criself;
11920253Sjoerg
12020253Sjoergstatic struct emulation *const emulations[] = { EMULATIONS };
12120253Sjoergstatic const int n_emulations = sizeof (emulations) / sizeof (emulations[0]);
12220253Sjoerg
12320253Sjoergstatic void select_emulation_mode PARAMS ((int, char **));
12420253Sjoerg
12520253Sjoergstatic void
12620253Sjoergselect_emulation_mode (argc, argv)
12720253Sjoerg     int argc;
12820253Sjoerg     char **argv;
12920253Sjoerg{
13020253Sjoerg  int i;
13120253Sjoerg  char *p, *em = 0;
13220253Sjoerg
13320253Sjoerg  for (i = 1; i < argc; i++)
13420253Sjoerg    if (!strncmp ("--em", argv[i], 4))
13520253Sjoerg      break;
13620253Sjoerg
13720253Sjoerg  if (i == argc)
13820253Sjoerg    goto do_default;
13920253Sjoerg
14020253Sjoerg  p = strchr (argv[i], '=');
14120253Sjoerg  if (p)
14220253Sjoerg    p++;
14320253Sjoerg  else
14420253Sjoerg    p = argv[i + 1];
14520253Sjoerg
14620253Sjoerg  if (!p || !*p)
14720253Sjoerg    as_fatal (_("missing emulation mode name"));
14820253Sjoerg  em = p;
14920253Sjoerg
15020253Sjoerg do_default:
15120253Sjoerg  if (em == 0)
15220253Sjoerg    em = getenv (EMULATION_ENVIRON);
15320253Sjoerg  if (em == 0)
15420253Sjoerg    em = DEFAULT_EMULATION;
15520253Sjoerg
15620253Sjoerg  if (em)
15720253Sjoerg    {
15820253Sjoerg      for (i = 0; i < n_emulations; i++)
15920253Sjoerg	if (!strcmp (emulations[i]->name, em))
16020253Sjoerg	  break;
16120253Sjoerg      if (i == n_emulations)
16220253Sjoerg	as_fatal (_("unrecognized emulation name `%s'"), em);
16320253Sjoerg      this_emulation = emulations[i];
16420253Sjoerg    }
16520253Sjoerg  else
16620253Sjoerg    this_emulation = emulations[0];
16720253Sjoerg
16820253Sjoerg  this_emulation->init ();
16920253Sjoerg}
17020253Sjoerg
17120253Sjoergconst char *
17220253Sjoergdefault_emul_bfd_name ()
17320253Sjoerg{
17420253Sjoerg  abort ();
17520253Sjoerg  return NULL;
17620253Sjoerg}
17720253Sjoerg
17820253Sjoergvoid
17920253Sjoergcommon_emul_init ()
18020253Sjoerg{
18120253Sjoerg  this_format = this_emulation->format;
18220253Sjoerg
18320253Sjoerg  if (this_emulation->leading_underscore == 2)
18420253Sjoerg    this_emulation->leading_underscore = this_format->dfl_leading_underscore;
18520253Sjoerg
18620253Sjoerg  if (this_emulation->default_endian != 2)
18720253Sjoerg    target_big_endian = this_emulation->default_endian;
18820253Sjoerg
18920253Sjoerg  if (this_emulation->fake_label_name == 0)
19020253Sjoerg    {
19120253Sjoerg      if (this_emulation->leading_underscore)
19220253Sjoerg	this_emulation->fake_label_name = "L0\001";
19320253Sjoerg      else
19420253Sjoerg	/* What other parameters should we test?  */
19520253Sjoerg	this_emulation->fake_label_name = ".L0\001";
19620253Sjoerg    }
19720253Sjoerg}
19820253Sjoerg#endif
19920253Sjoerg
20020253Sjoergvoid
20120253Sjoergprint_version_id ()
20220253Sjoerg{
20320253Sjoerg  static int printed;
20420253Sjoerg  if (printed)
20520253Sjoerg    return;
20620253Sjoerg  printed = 1;
20720253Sjoerg
20820253Sjoerg#ifdef BFD_ASSEMBLER
20920253Sjoerg  fprintf (stderr, _("GNU assembler version %s (%s) using BFD version %s"),
21020253Sjoerg	   VERSION, TARGET_ALIAS, BFD_VERSION);
21120747Sdavidn#else
21220253Sjoerg  fprintf (stderr, _("GNU assembler version %s (%s)"), VERSION, TARGET_ALIAS);
21320747Sdavidn#endif
21420253Sjoerg  fprintf (stderr, "\n");
21520253Sjoerg}
21620253Sjoerg
21720253Sjoergstatic void
21820253Sjoergshow_usage (stream)
21920747Sdavidn     FILE *stream;
22020747Sdavidn{
22120253Sjoerg  fprintf (stream, _("Usage: %s [option...] [asmfile...]\n"), myname);
22220253Sjoerg
22320253Sjoerg  fprintf (stream, _("\
22420747SdavidnOptions:\n\
22520747Sdavidn  -a[sub-option...]	  turn on listings\n\
22620253Sjoerg                      	  Sub-options [default hls]:\n\
22720747Sdavidn                      	  c      omit false conditionals\n\
22820747Sdavidn                      	  d      omit debugging directives\n\
22920747Sdavidn                      	  h      include high-level source\n\
23020253Sjoerg                      	  l      include assembly\n\
23120747Sdavidn                      	  m      include macro expansions\n\
23220747Sdavidn                      	  n      omit forms processing\n\
23320747Sdavidn                      	  s      include symbols\n\
23420747Sdavidn                      	  L      include line debug statistics (if applicable)\n\
23520747Sdavidn                      	  =FILE  list to FILE (must be last sub-option)\n"));
23620747Sdavidn
23720747Sdavidn  fprintf (stream, _("\
23820747Sdavidn  -D                      produce assembler debugging messages\n"));
23920747Sdavidn  fprintf (stream, _("\
24020747Sdavidn  --defsym SYM=VAL        define symbol SYM to given value\n"));
24120747Sdavidn#ifdef USE_EMULATIONS
24220253Sjoerg  {
24320747Sdavidn    int i;
24420253Sjoerg    char *def_em;
24520253Sjoerg
24620747Sdavidn    fprintf (stream, "\
24720747Sdavidn  --em=[");
24820747Sdavidn    for (i = 0; i < n_emulations - 1; i++)
24920747Sdavidn      fprintf (stream, "%s | ", emulations[i]->name);
25020747Sdavidn    fprintf (stream, "%s]\n", emulations[i]->name);
25120747Sdavidn
25220747Sdavidn    def_em = getenv (EMULATION_ENVIRON);
25320253Sjoerg    if (!def_em)
25420747Sdavidn      def_em = DEFAULT_EMULATION;
25520747Sdavidn    fprintf (stream, _("\
25620747Sdavidn                          emulate output (default %s)\n"), def_em);
25720747Sdavidn  }
25820253Sjoerg#endif
25920747Sdavidn  fprintf (stream, _("\
26020747Sdavidn  -f                      skip whitespace and comment preprocessing\n"));
26120747Sdavidn  fprintf (stream, _("\
26220747Sdavidn  --gstabs                generate stabs debugging information\n"));
26320747Sdavidn  fprintf (stream, _("\
26420747Sdavidn  --gdwarf2               generate DWARF2 debugging information\n"));
26520747Sdavidn  fprintf (stream, _("\
26620747Sdavidn  --help                  show this message and exit\n"));
26720747Sdavidn  fprintf (stream, _("\
26820747Sdavidn  --target-help           show target specific options\n"));
26920747Sdavidn  fprintf (stream, _("\
27020747Sdavidn  -I DIR                  add DIR to search list for .include directives\n"));
27120747Sdavidn  fprintf (stream, _("\
27220747Sdavidn  -J                      don't warn about signed overflow\n"));
27320747Sdavidn  fprintf (stream, _("\
27420747Sdavidn  -K                      warn when differences altered for long displacements\n"));
27520747Sdavidn  fprintf (stream, _("\
27620747Sdavidn  -L,--keep-locals        keep local symbols (e.g. starting with `L')\n"));
27720747Sdavidn  fprintf (stream, _("\
27820747Sdavidn  -M,--mri                assemble in MRI compatibility mode\n"));
27920747Sdavidn  fprintf (stream, _("\
28020747Sdavidn  --MD FILE               write dependency information in FILE (default none)\n"));
28120747Sdavidn  fprintf (stream, _("\
28220747Sdavidn  -nocpp                  ignored\n"));
28320747Sdavidn  fprintf (stream, _("\
28420747Sdavidn  -o OBJFILE              name the object-file output OBJFILE (default a.out)\n"));
28520747Sdavidn  fprintf (stream, _("\
28620747Sdavidn  -R                      fold data section into text section\n"));
28720747Sdavidn  fprintf (stream, _("\
28820747Sdavidn  --statistics            print various measured statistics from execution\n"));
28920747Sdavidn  fprintf (stream, _("\
29020747Sdavidn  --strip-local-absolute  strip local absolute symbols\n"));
29120747Sdavidn  fprintf (stream, _("\
29220747Sdavidn  --traditional-format    Use same format as native assembler when possible\n"));
29320747Sdavidn  fprintf (stream, _("\
29420747Sdavidn  --version               print assembler version number and exit\n"));
29520747Sdavidn  fprintf (stream, _("\
29620747Sdavidn  -W  --no-warn           suppress warnings\n"));
29720747Sdavidn  fprintf (stream, _("\
29820747Sdavidn  --warn                  don't suppress warnings\n"));
29920747Sdavidn  fprintf (stream, _("\
30020747Sdavidn  --fatal-warnings        treat warnings as errors\n"));
30120747Sdavidn  fprintf (stream, _("\
30220747Sdavidn  --itbl INSTTBL          extend instruction set to include instructions\n\
30320747Sdavidn                          matching the specifications defined in file INSTTBL\n"));
30420747Sdavidn  fprintf (stream, _("\
30520747Sdavidn  -w                      ignored\n"));
30620747Sdavidn  fprintf (stream, _("\
30720747Sdavidn  -X                      ignored\n"));
30820253Sjoerg  fprintf (stream, _("\
30920747Sdavidn  -Z                      generate object file even after errors\n"));
31020747Sdavidn  fprintf (stream, _("\
31120747Sdavidn  --listing-lhs-width     set the width in words of the output data column of\n\
31220747Sdavidn                          the listing\n"));
31320747Sdavidn  fprintf (stream, _("\
31420747Sdavidn  --listing-lhs-width2    set the width in words of the continuation lines\n\
31520747Sdavidn                          of the output data column; ignored if smaller than\n\
31620747Sdavidn                          the width of the first line\n"));
31720747Sdavidn  fprintf (stream, _("\
31820747Sdavidn  --listing-rhs-width     set the max width in characters of the lines from\n\
31920747Sdavidn                          the source file\n"));
32020747Sdavidn  fprintf (stream, _("\
32120747Sdavidn  --listing-cont-lines    set the maximum number of continuation lines used\n\
32220747Sdavidn                          for the output data column of the listing\n"));
32320747Sdavidn
32420747Sdavidn  md_show_usage (stream);
32520747Sdavidn
32620747Sdavidn  fputc ('\n', stream);
32720747Sdavidn  fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
32820747Sdavidn}
32920747Sdavidn
33020747Sdavidn/* Since it is easy to do here we interpret the special arg "-"
33120747Sdavidn   to mean "use stdin" and we set that argv[] pointing to "".
33220747Sdavidn   After we have munged argv[], the only things left are source file
33320747Sdavidn   name(s) and ""(s) denoting stdin. These file names are used
33420747Sdavidn   (perhaps more than once) later.
33520747Sdavidn
33620747Sdavidn   check for new machine-dep cmdline options in
33720747Sdavidn   md_parse_option definitions in config/tc-*.c.  */
33820747Sdavidn
33920747Sdavidnstatic void
34020747Sdavidnparse_args (pargc, pargv)
34120747Sdavidn     int *pargc;
34220747Sdavidn     char ***pargv;
34320747Sdavidn{
34420253Sjoerg  int old_argc, new_argc;
34520253Sjoerg  char **old_argv, **new_argv;
34620253Sjoerg
34720747Sdavidn  /* Starting the short option string with '-' is for programs that
34820253Sjoerg     expect options and other ARGV-elements in any order and that care about
34920253Sjoerg     the ordering of the two.  We describe each non-option ARGV-element
35020253Sjoerg     as if it were the argument of an option with character code 1.  */
35120253Sjoerg
35220253Sjoerg  char *shortopts;
35320253Sjoerg  extern CONST char *md_shortopts;
35420253Sjoerg  static const char std_shortopts[] = {
35520253Sjoerg    '-', 'J',
35620253Sjoerg#ifndef WORKING_DOT_WORD
35720253Sjoerg    /* -K is not meaningful if .word is not being hacked.  */
35820253Sjoerg    'K',
35920253Sjoerg#endif
36020253Sjoerg    'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
36120253Sjoerg#ifndef VMS
36220253Sjoerg    /* -v takes an argument on VMS, so we don't make it a generic
36320253Sjoerg       option.  */
36420253Sjoerg    'v',
36520253Sjoerg#endif
36620253Sjoerg    'w', 'X',
36720253Sjoerg    /* New option for extending instruction set (see also --itbl below)  */
36820253Sjoerg    't', ':',
36920747Sdavidn    '\0'
37020747Sdavidn  };
37120253Sjoerg  struct option *longopts;
37220253Sjoerg  extern struct option md_longopts[];
37320253Sjoerg  extern size_t md_longopts_size;
37420253Sjoerg  static const struct option std_longopts[] = {
37520253Sjoerg#define OPTION_HELP (OPTION_STD_BASE)
37620253Sjoerg    {"help", no_argument, NULL, OPTION_HELP},
37720253Sjoerg    {"keep-locals", no_argument, NULL, 'L'},
37820253Sjoerg    {"mri", no_argument, NULL, 'M'},
37920253Sjoerg#define OPTION_NOCPP (OPTION_STD_BASE + 1)
38020253Sjoerg    {"nocpp", no_argument, NULL, OPTION_NOCPP},
38120253Sjoerg#define OPTION_STATISTICS (OPTION_STD_BASE + 2)
38220253Sjoerg    {"statistics", no_argument, NULL, OPTION_STATISTICS},
38320253Sjoerg#define OPTION_VERSION (OPTION_STD_BASE + 3)
38420253Sjoerg    {"version", no_argument, NULL, OPTION_VERSION},
38520253Sjoerg#define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
38620253Sjoerg    {"dump-config", no_argument, NULL, OPTION_DUMPCONFIG},
38720253Sjoerg#define OPTION_VERBOSE (OPTION_STD_BASE + 5)
38820253Sjoerg    {"verbose", no_argument, NULL, OPTION_VERBOSE},
38920253Sjoerg#define OPTION_EMULATION (OPTION_STD_BASE + 6)
39020253Sjoerg    {"emulation", required_argument, NULL, OPTION_EMULATION},
39120253Sjoerg#define OPTION_DEFSYM (OPTION_STD_BASE + 7)
39220253Sjoerg    {"defsym", required_argument, NULL, OPTION_DEFSYM},
39320253Sjoerg#define OPTION_INSTTBL (OPTION_STD_BASE + 8)
39420253Sjoerg    /* New option for extending instruction set (see also -t above).
39520253Sjoerg       The "-t file" or "--itbl file" option extends the basic set of
39620253Sjoerg       valid instructions by reading "file", a text file containing a
39720253Sjoerg       list of instruction formats.  The additional opcodes and their
39820253Sjoerg       formats are added to the built-in set of instructions, and
39920253Sjoerg       mnemonics for new registers may also be defined.  */
40020253Sjoerg    {"itbl", required_argument, NULL, OPTION_INSTTBL},
40120253Sjoerg#define OPTION_LISTING_LHS_WIDTH (OPTION_STD_BASE + 9)
40220253Sjoerg    {"listing-lhs-width", required_argument, NULL, OPTION_LISTING_LHS_WIDTH},
40320747Sdavidn#define OPTION_LISTING_LHS_WIDTH2 (OPTION_STD_BASE + 10)
40420747Sdavidn    {"listing-lhs-width", required_argument, NULL, OPTION_LISTING_LHS_WIDTH2},
40520747Sdavidn#define OPTION_LISTING_RHS_WIDTH (OPTION_STD_BASE + 11)
40620747Sdavidn    {"listing-rhs-width", required_argument, NULL, OPTION_LISTING_RHS_WIDTH},
40720747Sdavidn#define OPTION_LISTING_CONT_LINES (OPTION_STD_BASE + 12)
40820747Sdavidn    {"listing-cont-lines", required_argument, NULL, OPTION_LISTING_CONT_LINES},
40920747Sdavidn#define OPTION_DEPFILE (OPTION_STD_BASE + 13)
41020747Sdavidn    {"MD", required_argument, NULL, OPTION_DEPFILE},
41120253Sjoerg#define OPTION_GSTABS (OPTION_STD_BASE + 14)
41220253Sjoerg    {"gstabs", no_argument, NULL, OPTION_GSTABS},
41320253Sjoerg#define OPTION_STRIP_LOCAL_ABSOLUTE (OPTION_STD_BASE + 15)
41420253Sjoerg    {"strip-local-absolute", no_argument, NULL, OPTION_STRIP_LOCAL_ABSOLUTE},
41520253Sjoerg#define OPTION_TRADITIONAL_FORMAT (OPTION_STD_BASE + 16)
41620253Sjoerg    {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
41720253Sjoerg#define OPTION_GDWARF2 (OPTION_STD_BASE + 17)
41820253Sjoerg    {"gdwarf2", no_argument, NULL, OPTION_GDWARF2},
41920253Sjoerg    {"no-warn", no_argument, NULL, 'W'},
42020747Sdavidn#define OPTION_WARN (OPTION_STD_BASE + 18)
42120747Sdavidn    {"warn", no_argument, NULL, OPTION_WARN},
42220747Sdavidn#define OPTION_TARGET_HELP (OPTION_STD_BASE + 19)
42320747Sdavidn    {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
42420747Sdavidn#define OPTION_WARN_FATAL (OPTION_STD_BASE + 20)
42520747Sdavidn    {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL}
42620747Sdavidn    /* When you add options here, check that they do not collide with
42720747Sdavidn       OPTION_MD_BASE.  See as.h.  */
42820747Sdavidn  };
42920253Sjoerg
43020253Sjoerg  /* Construct the option lists from the standard list and the target
43120253Sjoerg     dependent list.  Include space for an extra NULL option and
43220253Sjoerg     always NULL terminate.  */
43320253Sjoerg  shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
43420253Sjoerg  longopts = (struct option *) xmalloc (sizeof (std_longopts)
43520253Sjoerg					+ md_longopts_size
43620253Sjoerg					+ sizeof (struct option));
43720253Sjoerg  memcpy (longopts, std_longopts, sizeof (std_longopts));
43820253Sjoerg  memcpy ((char *) longopts + sizeof (std_longopts),
43920253Sjoerg	  md_longopts, md_longopts_size);
44020253Sjoerg  memset ((char *) longopts + sizeof (std_longopts) + md_longopts_size,
44120253Sjoerg	  0, sizeof (struct option));
44220253Sjoerg
44320253Sjoerg  /* Make a local copy of the old argv.  */
44420253Sjoerg  old_argc = *pargc;
44520253Sjoerg  old_argv = *pargv;
44620253Sjoerg
44720253Sjoerg  /* Initialize a new argv that contains no options.  */
44820253Sjoerg  new_argv = (char **) xmalloc (sizeof (char *) * (old_argc + 1));
44920253Sjoerg  new_argv[0] = old_argv[0];
45020253Sjoerg  new_argc = 1;
45120253Sjoerg  new_argv[new_argc] = NULL;
45220253Sjoerg
45320253Sjoerg  while (1)
45420253Sjoerg    {
45520253Sjoerg      /* getopt_long_only is like getopt_long, but '-' as well as '--' can
45620253Sjoerg	 indicate a long option.  */
45720253Sjoerg      int longind;
45820253Sjoerg      int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
45920253Sjoerg				   &longind);
46020253Sjoerg
46120253Sjoerg      if (optc == -1)
46220253Sjoerg	break;
46320253Sjoerg
46420253Sjoerg      switch (optc)
46520253Sjoerg	{
46620253Sjoerg	default:
46720253Sjoerg	  /* md_parse_option should return 1 if it recognizes optc,
46820253Sjoerg	     0 if not.  */
46920253Sjoerg	  if (md_parse_option (optc, optarg) != 0)
47020253Sjoerg	    break;
47120253Sjoerg	  /* `-v' isn't included in the general short_opts list, so check for
47220253Sjoerg	     it explicity here before deciding we've gotten a bad argument.  */
47320253Sjoerg	  if (optc == 'v')
47420253Sjoerg	    {
47520747Sdavidn#ifdef VMS
47620253Sjoerg	      /* Telling getopt to treat -v's value as optional can result
47720253Sjoerg		 in it picking up a following filename argument here.  The
47820253Sjoerg		 VMS code in md_parse_option can return 0 in that case,
47920253Sjoerg		 but it has no way of pushing the filename argument back.  */
48020253Sjoerg	      if (optarg && *optarg)
481		new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
482	      else
483#else
484	      case 'v':
485#endif
486	      case OPTION_VERBOSE:
487		print_version_id ();
488	      break;
489	    }
490	  /* Fall through.  */
491
492	case '?':
493	  exit (EXIT_FAILURE);
494
495	case 1:			/* File name.  */
496	  if (!strcmp (optarg, "-"))
497	    optarg = "";
498	  new_argv[new_argc++] = optarg;
499	  new_argv[new_argc] = NULL;
500	  break;
501
502	case OPTION_TARGET_HELP:
503          md_show_usage (stdout);
504          exit (EXIT_SUCCESS);
505
506	case OPTION_HELP:
507	  show_usage (stdout);
508	  exit (EXIT_SUCCESS);
509
510	case OPTION_NOCPP:
511	  break;
512
513	case OPTION_STATISTICS:
514	  flag_print_statistics = 1;
515	  break;
516
517	case OPTION_STRIP_LOCAL_ABSOLUTE:
518	  flag_strip_local_absolute = 1;
519	  break;
520
521	case OPTION_TRADITIONAL_FORMAT:
522	  flag_traditional_format = 1;
523	  break;
524
525	case OPTION_VERSION:
526	  /* This output is intended to follow the GNU standards document.  */
527	  printf (_("GNU assembler %s\n"), VERSION);
528	  printf (_("Copyright 2001 Free Software Foundation, Inc.\n"));
529	  printf (_("\
530This program is free software; you may redistribute it under the terms of\n\
531the GNU General Public License.  This program has absolutely no warranty.\n"));
532	  printf (_("This assembler was configured for a target of `%s'.\n"),
533		  TARGET_ALIAS);
534	  exit (EXIT_SUCCESS);
535
536	case OPTION_EMULATION:
537#ifdef USE_EMULATIONS
538	  if (strcmp (optarg, this_emulation->name))
539	    as_fatal (_("multiple emulation names specified"));
540#else
541	  as_fatal (_("emulations not handled in this configuration"));
542#endif
543	  break;
544
545	case OPTION_DUMPCONFIG:
546	  fprintf (stderr, _("alias = %s\n"), TARGET_ALIAS);
547	  fprintf (stderr, _("canonical = %s\n"), TARGET_CANONICAL);
548	  fprintf (stderr, _("cpu-type = %s\n"), TARGET_CPU);
549#ifdef TARGET_OBJ_FORMAT
550	  fprintf (stderr, _("format = %s\n"), TARGET_OBJ_FORMAT);
551#endif
552#ifdef TARGET_FORMAT
553	  fprintf (stderr, _("bfd-target = %s\n"), TARGET_FORMAT);
554#endif
555	  exit (EXIT_SUCCESS);
556
557	case OPTION_DEFSYM:
558	  {
559	    char *s;
560	    valueT i;
561	    struct defsym_list *n;
562
563	    for (s = optarg; *s != '\0' && *s != '='; s++)
564	      ;
565	    if (*s == '\0')
566	      as_fatal (_("bad defsym; format is --defsym name=value"));
567	    *s++ = '\0';
568#ifdef BFD_ASSEMBLER
569	    i = bfd_scan_vma (s, (const char **) NULL, 0);
570#else
571	    i = strtol (s, (char **) NULL, 0);
572#endif
573	    n = (struct defsym_list *) xmalloc (sizeof *n);
574	    n->next = defsyms;
575	    n->name = optarg;
576	    n->value = i;
577	    defsyms = n;
578	  }
579	  break;
580
581	case OPTION_INSTTBL:
582	case 't':
583	  {
584	    /* optarg is the name of the file containing the instruction
585	       formats, opcodes, register names, etc.  */
586	    struct itbl_file_list *n;
587
588	    if (optarg == NULL)
589	      {
590		as_warn (_("No file name following -t option\n"));
591		break;
592	      }
593
594	    n = (struct itbl_file_list *) xmalloc (sizeof *n);
595	    n->next = itbl_files;
596	    n->name = optarg;
597	    itbl_files = n;
598
599	    /* Parse the file and add the new instructions to our internal
600	       table.  If multiple instruction tables are specified, the
601	       information from this table gets appended onto the existing
602	       internal table.  */
603	    itbl_files->name = xstrdup (optarg);
604	    if (itbl_parse (itbl_files->name) != 0)
605	      {
606		fprintf (stderr, _("Failed to read instruction table %s\n"),
607			 itbl_files->name);
608		exit (EXIT_SUCCESS);
609	      }
610	  }
611	  break;
612
613	case OPTION_DEPFILE:
614	  start_dependencies (optarg);
615	  break;
616
617	case OPTION_GSTABS:
618	  debug_type = DEBUG_STABS;
619	  break;
620
621	case OPTION_GDWARF2:
622	  debug_type = DEBUG_DWARF2;
623	  break;
624
625	case 'J':
626	  flag_signed_overflow_ok = 1;
627	  break;
628
629#ifndef WORKING_DOT_WORD
630	case 'K':
631	  flag_warn_displacement = 1;
632	  break;
633#endif
634
635	case 'L':
636	  flag_keep_locals = 1;
637	  break;
638
639	case OPTION_LISTING_LHS_WIDTH:
640	  listing_lhs_width = atoi (optarg);
641	  if (listing_lhs_width_second < listing_lhs_width)
642	    listing_lhs_width_second = listing_lhs_width;
643	  break;
644	case OPTION_LISTING_LHS_WIDTH2:
645	  {
646	    int tmp = atoi (optarg);
647	    if (tmp > listing_lhs_width)
648	      listing_lhs_width_second = tmp;
649	  }
650	  break;
651	case OPTION_LISTING_RHS_WIDTH:
652	  listing_rhs_width = atoi (optarg);
653	  break;
654	case OPTION_LISTING_CONT_LINES:
655	  listing_lhs_cont_lines = atoi (optarg);
656	  break;
657
658	case 'M':
659	  flag_mri = 1;
660#ifdef TC_M68K
661	  flag_m68k_mri = 1;
662#endif
663	  break;
664
665	case 'R':
666	  flag_readonly_data_in_text = 1;
667	  break;
668
669	case 'W':
670	  flag_no_warnings = 1;
671	  break;
672
673	case OPTION_WARN:
674	  flag_no_warnings = 0;
675	  flag_fatal_warnings = 0;
676	  break;
677
678	case OPTION_WARN_FATAL:
679	  flag_no_warnings = 0;
680	  flag_fatal_warnings = 1;
681	  break;
682
683	case 'Z':
684	  flag_always_generate_output = 1;
685	  break;
686
687	case 'a':
688	  if (optarg)
689	    {
690	      if (md_parse_option (optc, optarg) != 0)
691		break;
692
693	      while (*optarg)
694		{
695		  switch (*optarg)
696		    {
697		    case 'c':
698		      listing |= LISTING_NOCOND;
699		      break;
700		    case 'd':
701		      listing |= LISTING_NODEBUG;
702		      break;
703		    case 'h':
704		      listing |= LISTING_HLL;
705		      break;
706		    case 'l':
707		      listing |= LISTING_LISTING;
708		      break;
709		    case 'm':
710		      listing |= LISTING_MACEXP;
711		      break;
712		    case 'n':
713		      listing |= LISTING_NOFORM;
714		      break;
715		    case 's':
716		      listing |= LISTING_SYMBOLS;
717		      break;
718		    case '=':
719		      listing_filename = xstrdup (optarg + 1);
720		      optarg += strlen (listing_filename);
721		      break;
722		    default:
723		      as_fatal (_("invalid listing option `%c'"), *optarg);
724		      break;
725		    }
726		  optarg++;
727		}
728	    }
729	  if (!listing)
730	    listing = LISTING_DEFAULT;
731	  break;
732
733	case 'D':
734	  /* DEBUG is implemented: it debugs different
735	     things from other people's assemblers.  */
736	  flag_debug = 1;
737	  break;
738
739	case 'f':
740	  flag_no_comments = 1;
741	  break;
742
743	case 'I':
744	  {			/* Include file directory.  */
745	    char *temp = xstrdup (optarg);
746	    add_include_dir (temp);
747	    break;
748	  }
749
750	case 'o':
751	  out_file_name = xstrdup (optarg);
752	  break;
753
754	case 'w':
755	  break;
756
757	case 'X':
758	  /* -X means treat warnings as errors.  */
759	  break;
760	}
761    }
762
763  free (shortopts);
764  free (longopts);
765
766  *pargc = new_argc;
767  *pargv = new_argv;
768}
769
770static long start_time;
771
772int
773main (argc, argv)
774     int argc;
775     char **argv;
776{
777  int macro_alternate;
778  int macro_strip_at;
779  int keep_it;
780
781  start_time = get_run_time ();
782
783#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
784  setlocale (LC_MESSAGES, "");
785#endif
786  bindtextdomain (PACKAGE, LOCALEDIR);
787  textdomain (PACKAGE);
788
789  if (debug_memory)
790    {
791#ifdef BFD_ASSEMBLER
792      extern long _bfd_chunksize;
793      _bfd_chunksize = 64;
794#endif
795      chunksize = 64;
796    }
797
798#ifdef HOST_SPECIAL_INIT
799  HOST_SPECIAL_INIT (argc, argv);
800#endif
801
802  myname = argv[0];
803  xmalloc_set_program_name (myname);
804
805  START_PROGRESS (myname, 0);
806
807#ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
808#define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
809#endif
810
811  out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
812
813  hex_init ();
814#ifdef BFD_ASSEMBLER
815  bfd_init ();
816  bfd_set_error_program_name (myname);
817#endif
818
819#ifdef USE_EMULATIONS
820  select_emulation_mode (argc, argv);
821#endif
822
823  PROGRESS (1);
824  symbol_begin ();
825  frag_init ();
826  subsegs_begin ();
827  parse_args (&argc, &argv);
828  read_begin ();
829  input_scrub_begin ();
830  expr_begin ();
831
832  if (flag_print_statistics)
833    xatexit (dump_statistics);
834
835  macro_alternate = 0;
836  macro_strip_at = 0;
837#ifdef TC_I960
838  macro_strip_at = flag_mri;
839#endif
840#ifdef TC_A29K
841  /* For compatibility with the AMD 29K family macro assembler
842     specification.  */
843  macro_alternate = 1;
844  macro_strip_at = 1;
845#endif
846
847  macro_init (macro_alternate, flag_mri, macro_strip_at, macro_expr);
848
849  PROGRESS (1);
850
851#ifdef BFD_ASSEMBLER
852  output_file_create (out_file_name);
853  assert (stdoutput != 0);
854#endif
855
856#ifdef tc_init_after_args
857  tc_init_after_args ();
858#endif
859
860  itbl_init ();
861
862  /* Now that we have fully initialized, and have created the output
863     file, define any symbols requested by --defsym command line
864     arguments.  */
865  while (defsyms != NULL)
866    {
867      symbolS *sym;
868      struct defsym_list *next;
869
870      sym = symbol_new (defsyms->name, absolute_section, defsyms->value,
871			&zero_address_frag);
872      symbol_table_insert (sym);
873      next = defsyms->next;
874      free (defsyms);
875      defsyms = next;
876    }
877
878  PROGRESS (1);
879
880  /* Assemble it.  */
881  perform_an_assembly_pass (argc, argv);
882
883  cond_finish_check (-1);
884
885#ifdef md_end
886  md_end ();
887#endif
888
889  /* If we've been collecting dwarf2 .debug_line info, either for
890     assembly debugging or on behalf of the compiler, emit it now.  */
891  dwarf2_finish ();
892
893  if (seen_at_least_1_file ()
894      && (flag_always_generate_output || had_errors () == 0))
895    keep_it = 1;
896  else
897    keep_it = 0;
898
899#if defined (BFD_ASSEMBLER) || !defined (BFD)
900  /* This used to be done at the start of write_object_file in
901     write.c, but that caused problems when doing listings when
902     keep_it was zero.  This could probably be moved above md_end, but
903     I didn't want to risk the change.  */
904  subsegs_finish ();
905#endif
906
907  if (keep_it)
908    write_object_file ();
909
910#ifndef NO_LISTING
911  listing_print (listing_filename);
912#endif
913
914#ifndef OBJ_VMS /* does its own file handling */
915#ifndef BFD_ASSEMBLER
916  if (keep_it)
917#endif
918    output_file_close (out_file_name);
919#endif
920
921  if (flag_fatal_warnings && had_warnings () > 0 && had_errors () == 0)
922    as_bad (_("%d warnings, treating warnings as errors"), had_warnings ());
923
924  if (had_errors () > 0 && ! flag_always_generate_output)
925    keep_it = 0;
926
927  if (!keep_it)
928    unlink (out_file_name);
929
930  input_scrub_end ();
931
932  END_PROGRESS (myname);
933
934  /* Use xexit instead of return, because under VMS environments they
935     may not place the same interpretation on the value given.  */
936  if (had_errors () > 0)
937    xexit (EXIT_FAILURE);
938
939  /* Only generate dependency file if assembler was successful.  */
940  print_dependencies ();
941
942  xexit (EXIT_SUCCESS);
943}
944
945static void
946dump_statistics ()
947{
948#ifdef HAVE_SBRK
949  char *lim = (char *) sbrk (0);
950#endif
951  long run_time = get_run_time () - start_time;
952
953  fprintf (stderr, _("%s: total time in assembly: %ld.%06ld\n"),
954	   myname, run_time / 1000000, run_time % 1000000);
955#ifdef HAVE_SBRK
956  fprintf (stderr, _("%s: data size %ld\n"),
957	   myname, (long) (lim - (char *) &environ));
958#endif
959
960  subsegs_print_statistics (stderr);
961  write_print_statistics (stderr);
962  symbol_print_statistics (stderr);
963  read_print_statistics (stderr);
964
965#ifdef tc_print_statistics
966  tc_print_statistics (stderr);
967#endif
968#ifdef obj_print_statistics
969  obj_print_statistics (stderr);
970#endif
971}
972
973/* Here to attempt 1 pass over each input file.
974   We scan argv[*] looking for filenames or exactly "" which is
975   shorthand for stdin. Any argv that is NULL is not a file-name.
976   We set need_pass_2 TRUE if, after this, we still have unresolved
977   expressions of the form (unknown value)+-(unknown value).
978
979   Note the un*x semantics: there is only 1 logical input file, but it
980   may be a catenation of many 'physical' input files.  */
981
982static void
983perform_an_assembly_pass (argc, argv)
984     int argc;
985     char **argv;
986{
987  int saw_a_file = 0;
988#ifdef BFD_ASSEMBLER
989  flagword applicable;
990#endif
991
992  need_pass_2 = 0;
993
994#ifndef BFD_ASSEMBLER
995#ifdef MANY_SEGMENTS
996  {
997    unsigned int i;
998    for (i = SEG_E0; i < SEG_UNKNOWN; i++)
999      segment_info[i].fix_root = 0;
1000  }
1001  /* Create the three fixed ones.  */
1002  {
1003    segT seg;
1004
1005#ifdef TE_APOLLO
1006    seg = subseg_new (".wtext", 0);
1007#else
1008    seg = subseg_new (".text", 0);
1009#endif
1010    assert (seg == SEG_E0);
1011    seg = subseg_new (".data", 0);
1012    assert (seg == SEG_E1);
1013    seg = subseg_new (".bss", 0);
1014    assert (seg == SEG_E2);
1015#ifdef TE_APOLLO
1016    create_target_segments ();
1017#endif
1018  }
1019
1020#else /* not MANY_SEGMENTS */
1021  text_fix_root = NULL;
1022  data_fix_root = NULL;
1023  bss_fix_root = NULL;
1024#endif /* not MANY_SEGMENTS */
1025#else /* BFD_ASSEMBLER */
1026  /* Create the standard sections, and those the assembler uses
1027     internally.  */
1028  text_section = subseg_new (TEXT_SECTION_NAME, 0);
1029  data_section = subseg_new (DATA_SECTION_NAME, 0);
1030  bss_section = subseg_new (BSS_SECTION_NAME, 0);
1031  /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
1032     to have relocs, otherwise we don't find out in time.  */
1033  applicable = bfd_applicable_section_flags (stdoutput);
1034  bfd_set_section_flags (stdoutput, text_section,
1035			 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1036				       | SEC_CODE | SEC_READONLY));
1037  bfd_set_section_flags (stdoutput, data_section,
1038			 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1039				       | SEC_DATA));
1040  bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
1041  seg_info (bss_section)->bss = 1;
1042  subseg_new (BFD_ABS_SECTION_NAME, 0);
1043  subseg_new (BFD_UND_SECTION_NAME, 0);
1044  reg_section = subseg_new ("*GAS `reg' section*", 0);
1045  expr_section = subseg_new ("*GAS `expr' section*", 0);
1046
1047#endif /* BFD_ASSEMBLER */
1048
1049  subseg_set (text_section, 0);
1050
1051  /* This may add symbol table entries, which requires having an open BFD,
1052     and sections already created, in BFD_ASSEMBLER mode.  */
1053  md_begin ();
1054
1055#ifdef obj_begin
1056  obj_begin ();
1057#endif
1058
1059  /* Skip argv[0].  */
1060  argv++;
1061  argc--;
1062
1063  while (argc--)
1064    {
1065      if (*argv)
1066	{			/* Is it a file-name argument?  */
1067	  PROGRESS (1);
1068	  saw_a_file++;
1069	  /* argv->"" if stdin desired, else->filename  */
1070	  read_a_source_file (*argv);
1071	}
1072      argv++;			/* completed that argv  */
1073    }
1074  if (!saw_a_file)
1075    read_a_source_file ("");
1076}
1077
1078/* The interface between the macro code and gas expression handling.  */
1079
1080static int
1081macro_expr (emsg, idx, in, val)
1082     const char *emsg;
1083     int idx;
1084     sb *in;
1085     int *val;
1086{
1087  char *hold;
1088  expressionS ex;
1089
1090  sb_terminate (in);
1091
1092  hold = input_line_pointer;
1093  input_line_pointer = in->ptr + idx;
1094  expression (&ex);
1095  idx = input_line_pointer - in->ptr;
1096  input_line_pointer = hold;
1097
1098  if (ex.X_op != O_constant)
1099    as_bad ("%s", emsg);
1100
1101  *val = (int) ex.X_add_number;
1102
1103  return idx;
1104}
1105