as.c revision 89857
1252190Srpaulo/* as.c - GAS main program.
2252190Srpaulo   Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3252190Srpaulo   1999, 2000, 2001, 2002
4252190Srpaulo   Free Software Foundation, Inc.
5252190Srpaulo
6252190Srpaulo   This file is part of GAS, the GNU Assembler.
7252190Srpaulo
8252190Srpaulo   GAS is free software; you can redistribute it and/or modify
9252190Srpaulo   it under the terms of the GNU General Public License as published by
10252190Srpaulo   the Free Software Foundation; either version 2, or (at your option)
11252190Srpaulo   any later version.
12252190Srpaulo
13252190Srpaulo   GAS is distributed in the hope that it will be useful,
14252190Srpaulo   but WITHOUT ANY WARRANTY; without even the implied warranty of
15252190Srpaulo   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16252190Srpaulo   GNU General Public License for more details.
17252190Srpaulo
18252190Srpaulo   You should have received a copy of the GNU General Public License
19252190Srpaulo   along with GAS; see the file COPYING.  If not, write to the Free
20252190Srpaulo   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21252190Srpaulo   02111-1307, USA.  */
22252190Srpaulo
23252190Srpaulo/* Main program for AS; a 32-bit assembler of GNU.
24252190Srpaulo * Understands command arguments.
25252190Srpaulo * Has a few routines that don't fit in other modules because they
26252190Srpaulo * are shared.
27281806Srpaulo *
28252190Srpaulo *			bugs
29252190Srpaulo *
30252190Srpaulo * : initialisers
31252190Srpaulo *	Since no-one else says they will support them in future: I
32281806Srpaulo * don't support them now.
33281806Srpaulo */
34252190Srpaulo
35252190Srpaulo#include "ansidecl.h"
36252190Srpaulo
37252190Srpaulo#define COMMON
38252190Srpaulo
39252190Srpaulo#include "as.h"
40252190Srpaulo#include "subsegs.h"
41252190Srpaulo#include "output-file.h"
42252190Srpaulo#include "sb.h"
43252190Srpaulo#include "macro.h"
44252190Srpaulo#include "dwarf2dbg.h"
45252190Srpaulo
46252190Srpaulo#ifdef HAVE_ITBL_CPU
47252190Srpaulo#include "itbl-ops.h"
48252190Srpaulo#else
49252190Srpaulo#define itbl_parse(itbl_file) 1
50252190Srpaulo#define itbl_init()
51252190Srpaulo#endif
52252190Srpaulo
53252190Srpaulo#ifdef HAVE_SBRK
54252190Srpaulo#ifdef NEED_DECLARATION_SBRK
55252190Srpauloextern PTR sbrk ();
56252190Srpaulo#endif
57252190Srpaulo#endif
58252190Srpaulo
59252190Srpaulostatic void show_usage PARAMS ((FILE *));
60252190Srpaulostatic void parse_args PARAMS ((int *, char ***));
61252190Srpaulostatic void dump_statistics PARAMS ((void));
62252190Srpaulostatic void perform_an_assembly_pass PARAMS ((int argc, char **argv));
63252190Srpaulostatic int macro_expr PARAMS ((const char *, int, sb *, int *));
64252190Srpaulo
65252190Srpaulo/* True if a listing is wanted.  */
66252190Srpauloint listing;
67252190Srpaulo
68252190Srpaulo/* Name of listing file.  */
69252190Srpaulostatic char *listing_filename = NULL;
70252190Srpaulo
71252190Srpaulo/* Type of debugging to generate.  */
72281806Srpaulo
73252190Srpauloenum debug_info_type debug_type = DEBUG_UNSPECIFIED;
74252190Srpaulo
75252190Srpaulo/* Maximum level of macro nesting.  */
76252190Srpauloint max_macro_nest = 100;
77252190Srpaulo
78252190Srpaulo/* argv[0]  */
79252190Srpaulochar *myname;
80252190Srpaulo#ifdef BFD_ASSEMBLER
81252190SrpaulosegT reg_section, expr_section;
82252190SrpaulosegT text_section, data_section, bss_section;
83252190Srpaulo#endif
84252190Srpaulo
85252190Srpaulo/* The default obstack chunk size.  If we set this to zero, the
86252190Srpaulo   obstack code will use whatever will fit in a 4096 byte block.  */
87252190Srpauloint chunksize = 0;
88252190Srpaulo
89252190Srpaulo/* To monitor memory allocation more effectively, make this non-zero.
90252190Srpaulo   Then the chunk sizes for gas and bfd will be reduced.  */
91252190Srpauloint debug_memory = 0;
92252190Srpaulo
93252190Srpaulo/* We build a list of defsyms as we read the options, and then define
94252190Srpaulo   them after we have initialized everything.  */
95252190Srpaulo
96252190Srpaulostruct defsym_list {
97252190Srpaulo  struct defsym_list *next;
98252190Srpaulo  char *name;
99252190Srpaulo  valueT value;
100252190Srpaulo};
101252190Srpaulo
102252190Srpaulostatic struct defsym_list *defsyms;
103252190Srpaulo
104252190Srpaulo/* Keep a record of the itbl files we read in.  */
105252190Srpaulo
106252190Srpaulostruct itbl_file_list {
107252190Srpaulo  struct itbl_file_list *next;
108252190Srpaulo  char *name;
109252190Srpaulo};
110281806Srpaulo
111252190Srpaulostatic struct itbl_file_list *itbl_files;
112252190Srpaulo
113252190Srpaulo#ifdef USE_EMULATIONS
114252190Srpaulo#define EMULATION_ENVIRON "AS_EMULATION"
115252190Srpaulo
116252190Srpauloextern struct emulation mipsbelf, mipslelf, mipself;
117252190Srpauloextern struct emulation mipsbecoff, mipslecoff, mipsecoff;
118252190Srpauloextern struct emulation i386coff, i386elf, i386aout;
119252190Srpauloextern struct emulation crisaout, criself;
120252190Srpaulo
121252190Srpaulostatic struct emulation *const emulations[] = { EMULATIONS };
122252190Srpaulostatic const int n_emulations = sizeof (emulations) / sizeof (emulations[0]);
123252190Srpaulo
124252190Srpaulostatic void select_emulation_mode PARAMS ((int, char **));
125252190Srpaulo
126252190Srpaulostatic void
127252190Srpauloselect_emulation_mode (argc, argv)
128252190Srpaulo     int argc;
129252190Srpaulo     char **argv;
130252190Srpaulo{
131252190Srpaulo  int i;
132252190Srpaulo  char *p, *em = 0;
133252190Srpaulo
134252190Srpaulo  for (i = 1; i < argc; i++)
135252190Srpaulo    if (!strncmp ("--em", argv[i], 4))
136252190Srpaulo      break;
137252190Srpaulo
138252190Srpaulo  if (i == argc)
139252190Srpaulo    goto do_default;
140252190Srpaulo
141252190Srpaulo  p = strchr (argv[i], '=');
142252190Srpaulo  if (p)
143252190Srpaulo    p++;
144252190Srpaulo  else
145252190Srpaulo    p = argv[i + 1];
146252190Srpaulo
147252190Srpaulo  if (!p || !*p)
148252190Srpaulo    as_fatal (_("missing emulation mode name"));
149252190Srpaulo  em = p;
150252190Srpaulo
151252190Srpaulo do_default:
152252190Srpaulo  if (em == 0)
153252190Srpaulo    em = getenv (EMULATION_ENVIRON);
154252190Srpaulo  if (em == 0)
155252190Srpaulo    em = DEFAULT_EMULATION;
156252190Srpaulo
157252190Srpaulo  if (em)
158252190Srpaulo    {
159252190Srpaulo      for (i = 0; i < n_emulations; i++)
160252190Srpaulo	if (!strcmp (emulations[i]->name, em))
161252190Srpaulo	  break;
162252190Srpaulo      if (i == n_emulations)
163252190Srpaulo	as_fatal (_("unrecognized emulation name `%s'"), em);
164252190Srpaulo      this_emulation = emulations[i];
165252190Srpaulo    }
166252190Srpaulo  else
167281806Srpaulo    this_emulation = emulations[0];
168281806Srpaulo
169281806Srpaulo  this_emulation->init ();
170281806Srpaulo}
171281806Srpaulo
172281806Srpauloconst char *
173281806Srpaulodefault_emul_bfd_name ()
174281806Srpaulo{
175337817Scy  abort ();
176337817Scy  return NULL;
177281806Srpaulo}
178281806Srpaulo
179281806Srpaulovoid
180281806Srpaulocommon_emul_init ()
181281806Srpaulo{
182281806Srpaulo  this_format = this_emulation->format;
183281806Srpaulo
184281806Srpaulo  if (this_emulation->leading_underscore == 2)
185281806Srpaulo    this_emulation->leading_underscore = this_format->dfl_leading_underscore;
186281806Srpaulo
187281806Srpaulo  if (this_emulation->default_endian != 2)
188281806Srpaulo    target_big_endian = this_emulation->default_endian;
189281806Srpaulo
190281806Srpaulo  if (this_emulation->fake_label_name == 0)
191281806Srpaulo    {
192281806Srpaulo      if (this_emulation->leading_underscore)
193281806Srpaulo	this_emulation->fake_label_name = "L0\001";
194281806Srpaulo      else
195281806Srpaulo	/* What other parameters should we test?  */
196281806Srpaulo	this_emulation->fake_label_name = ".L0\001";
197281806Srpaulo    }
198281806Srpaulo}
199281806Srpaulo#endif
200281806Srpaulo
201281806Srpaulovoid
202281806Srpauloprint_version_id ()
203281806Srpaulo{
204281806Srpaulo  static int printed;
205281806Srpaulo  if (printed)
206281806Srpaulo    return;
207281806Srpaulo  printed = 1;
208281806Srpaulo
209281806Srpaulo#ifdef BFD_ASSEMBLER
210281806Srpaulo  fprintf (stderr, _("GNU assembler version %s (%s) using BFD version %s"),
211281806Srpaulo	   VERSION, TARGET_ALIAS, BFD_VERSION_STRING);
212337817Scy#else
213337817Scy  fprintf (stderr, _("GNU assembler version %s (%s)"), VERSION, TARGET_ALIAS);
214337817Scy#endif
215337817Scy  fprintf (stderr, "\n");
216281806Srpaulo}
217281806Srpaulo
218281806Srpaulostatic void
219281806Srpauloshow_usage (stream)
220281806Srpaulo     FILE *stream;
221337817Scy{
222337817Scy  fprintf (stream, _("Usage: %s [option...] [asmfile...]\n"), myname);
223337817Scy
224337817Scy  fprintf (stream, _("\
225337817ScyOptions:\n\
226337817Scy  -a[sub-option...]	  turn on listings\n\
227281806Srpaulo                      	  Sub-options [default hls]:\n\
228281806Srpaulo                      	  c      omit false conditionals\n\
229281806Srpaulo                      	  d      omit debugging directives\n\
230281806Srpaulo                      	  h      include high-level source\n\
231252190Srpaulo                      	  l      include assembly\n\
232252190Srpaulo                      	  m      include macro expansions\n\
233252190Srpaulo                      	  n      omit forms processing\n\
234281806Srpaulo                      	  s      include symbols\n\
235252190Srpaulo                      	  =FILE  list to FILE (must be last sub-option)\n"));
236252190Srpaulo
237252190Srpaulo  fprintf (stream, _("\
238252190Srpaulo  -D                      produce assembler debugging messages\n"));
239252190Srpaulo  fprintf (stream, _("\
240252190Srpaulo  --defsym SYM=VAL        define symbol SYM to given value\n"));
241252190Srpaulo#ifdef USE_EMULATIONS
242281806Srpaulo  {
243281806Srpaulo    int i;
244252190Srpaulo    char *def_em;
245252190Srpaulo
246337817Scy    fprintf (stream, "\
247337817Scy  --em=[");
248337817Scy    for (i = 0; i < n_emulations - 1; i++)
249252190Srpaulo      fprintf (stream, "%s | ", emulations[i]->name);
250252190Srpaulo    fprintf (stream, "%s]\n", emulations[i]->name);
251252190Srpaulo
252252190Srpaulo    def_em = getenv (EMULATION_ENVIRON);
253281806Srpaulo    if (!def_em)
254281806Srpaulo      def_em = DEFAULT_EMULATION;
255281806Srpaulo    fprintf (stream, _("\
256281806Srpaulo                          emulate output (default %s)\n"), def_em);
257337817Scy  }
258337817Scy#endif
259337817Scy  fprintf (stream, _("\
260337817Scy  -f                      skip whitespace and comment preprocessing\n"));
261281806Srpaulo  fprintf (stream, _("\
262281806Srpaulo  --gstabs                generate stabs debugging information\n"));
263252190Srpaulo  fprintf (stream, _("\
264252190Srpaulo  --gdwarf2               generate DWARF2 debugging information\n"));
265252190Srpaulo  fprintf (stream, _("\
266252190Srpaulo  --help                  show this message and exit\n"));
267252190Srpaulo  fprintf (stream, _("\
268281806Srpaulo  --target-help           show target specific options\n"));
269281806Srpaulo  fprintf (stream, _("\
270252190Srpaulo  -I DIR                  add DIR to search list for .include directives\n"));
271281806Srpaulo  fprintf (stream, _("\
272281806Srpaulo  -J                      don't warn about signed overflow\n"));
273281806Srpaulo  fprintf (stream, _("\
274281806Srpaulo  -K                      warn when differences altered for long displacements\n"));
275281806Srpaulo  fprintf (stream, _("\
276252190Srpaulo  -L,--keep-locals        keep local symbols (e.g. starting with `L')\n"));
277281806Srpaulo  fprintf (stream, _("\
278281806Srpaulo  -M,--mri                assemble in MRI compatibility mode\n"));
279281806Srpaulo  fprintf (stream, _("\
280281806Srpaulo  --MD FILE               write dependency information in FILE (default none)\n"));
281281806Srpaulo  fprintf (stream, _("\
282281806Srpaulo  -nocpp                  ignored\n"));
283252190Srpaulo  fprintf (stream, _("\
284281806Srpaulo  -o OBJFILE              name the object-file output OBJFILE (default a.out)\n"));
285281806Srpaulo  fprintf (stream, _("\
286252190Srpaulo  -R                      fold data section into text section\n"));
287252190Srpaulo  fprintf (stream, _("\
288281806Srpaulo  --statistics            print various measured statistics from execution\n"));
289281806Srpaulo  fprintf (stream, _("\
290281806Srpaulo  --strip-local-absolute  strip local absolute symbols\n"));
291281806Srpaulo  fprintf (stream, _("\
292252190Srpaulo  --traditional-format    Use same format as native assembler when possible\n"));
293281806Srpaulo  fprintf (stream, _("\
294281806Srpaulo  --version               print assembler version number and exit\n"));
295281806Srpaulo  fprintf (stream, _("\
296281806Srpaulo  -W  --no-warn           suppress warnings\n"));
297281806Srpaulo  fprintf (stream, _("\
298281806Srpaulo  --warn                  don't suppress warnings\n"));
299281806Srpaulo  fprintf (stream, _("\
300281806Srpaulo  --fatal-warnings        treat warnings as errors\n"));
301252190Srpaulo  fprintf (stream, _("\
302252190Srpaulo  --itbl INSTTBL          extend instruction set to include instructions\n\
303252190Srpaulo                          matching the specifications defined in file INSTTBL\n"));
304252190Srpaulo  fprintf (stream, _("\
305252190Srpaulo  -w                      ignored\n"));
306252190Srpaulo  fprintf (stream, _("\
307252190Srpaulo  -X                      ignored\n"));
308252190Srpaulo  fprintf (stream, _("\
309252190Srpaulo  -Z                      generate object file even after errors\n"));
310252190Srpaulo  fprintf (stream, _("\
311252190Srpaulo  --listing-lhs-width     set the width in words of the output data column of\n\
312252190Srpaulo                          the listing\n"));
313252190Srpaulo  fprintf (stream, _("\
314252190Srpaulo  --listing-lhs-width2    set the width in words of the continuation lines\n\
315252190Srpaulo                          of the output data column; ignored if smaller than\n\
316252190Srpaulo                          the width of the first line\n"));
317252190Srpaulo  fprintf (stream, _("\
318252190Srpaulo  --listing-rhs-width     set the max width in characters of the lines from\n\
319252190Srpaulo                          the source file\n"));
320252190Srpaulo  fprintf (stream, _("\
321252190Srpaulo  --listing-cont-lines    set the maximum number of continuation lines used\n\
322252190Srpaulo                          for the output data column of the listing\n"));
323252190Srpaulo
324252190Srpaulo  md_show_usage (stream);
325252190Srpaulo
326252190Srpaulo  fputc ('\n', stream);
327252190Srpaulo  fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
328252190Srpaulo}
329252190Srpaulo
330252190Srpaulo/* Since it is easy to do here we interpret the special arg "-"
331252190Srpaulo   to mean "use stdin" and we set that argv[] pointing to "".
332252190Srpaulo   After we have munged argv[], the only things left are source file
333252190Srpaulo   name(s) and ""(s) denoting stdin. These file names are used
334252190Srpaulo   (perhaps more than once) later.
335252190Srpaulo
336252190Srpaulo   check for new machine-dep cmdline options in
337252190Srpaulo   md_parse_option definitions in config/tc-*.c.  */
338252190Srpaulo
339252190Srpaulostatic void
340252190Srpauloparse_args (pargc, pargv)
341252190Srpaulo     int *pargc;
342252190Srpaulo     char ***pargv;
343252190Srpaulo{
344252190Srpaulo  int old_argc, new_argc;
345252190Srpaulo  char **old_argv, **new_argv;
346252190Srpaulo
347252190Srpaulo  /* Starting the short option string with '-' is for programs that
348252190Srpaulo     expect options and other ARGV-elements in any order and that care about
349252190Srpaulo     the ordering of the two.  We describe each non-option ARGV-element
350252190Srpaulo     as if it were the argument of an option with character code 1.  */
351252190Srpaulo
352252190Srpaulo  char *shortopts;
353252190Srpaulo  extern CONST char *md_shortopts;
354252190Srpaulo  static const char std_shortopts[] = {
355252190Srpaulo    '-', 'J',
356252190Srpaulo#ifndef WORKING_DOT_WORD
357252190Srpaulo    /* -K is not meaningful if .word is not being hacked.  */
358252190Srpaulo    'K',
359252190Srpaulo#endif
360252190Srpaulo    'L', 'M', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
361252190Srpaulo#ifndef VMS
362252190Srpaulo    /* -v takes an argument on VMS, so we don't make it a generic
363252190Srpaulo       option.  */
364252190Srpaulo    'v',
365252190Srpaulo#endif
366252190Srpaulo    'w', 'X',
367252190Srpaulo    /* New option for extending instruction set (see also --itbl below)  */
368252190Srpaulo    't', ':',
369252190Srpaulo    '\0'
370252190Srpaulo  };
371252190Srpaulo  struct option *longopts;
372252190Srpaulo  extern struct option md_longopts[];
373252190Srpaulo  extern size_t md_longopts_size;
374252190Srpaulo  static const struct option std_longopts[] = {
375252190Srpaulo#define OPTION_HELP (OPTION_STD_BASE)
376252190Srpaulo    {"help", no_argument, NULL, OPTION_HELP},
377252190Srpaulo    {"keep-locals", no_argument, NULL, 'L'},
378252190Srpaulo    {"mri", no_argument, NULL, 'M'},
379252190Srpaulo#define OPTION_NOCPP (OPTION_STD_BASE + 1)
380252190Srpaulo    {"nocpp", no_argument, NULL, OPTION_NOCPP},
381252190Srpaulo#define OPTION_STATISTICS (OPTION_STD_BASE + 2)
382252190Srpaulo    {"statistics", no_argument, NULL, OPTION_STATISTICS},
383252190Srpaulo#define OPTION_VERSION (OPTION_STD_BASE + 3)
384252190Srpaulo    {"version", no_argument, NULL, OPTION_VERSION},
385252190Srpaulo#define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
386252190Srpaulo    {"dump-config", no_argument, NULL, OPTION_DUMPCONFIG},
387252190Srpaulo#define OPTION_VERBOSE (OPTION_STD_BASE + 5)
388252190Srpaulo    {"verbose", no_argument, NULL, OPTION_VERBOSE},
389252190Srpaulo#define OPTION_EMULATION (OPTION_STD_BASE + 6)
390252190Srpaulo    {"emulation", required_argument, NULL, OPTION_EMULATION},
391252190Srpaulo#define OPTION_DEFSYM (OPTION_STD_BASE + 7)
392252190Srpaulo    {"defsym", required_argument, NULL, OPTION_DEFSYM},
393252190Srpaulo#define OPTION_INSTTBL (OPTION_STD_BASE + 8)
394252190Srpaulo    /* New option for extending instruction set (see also -t above).
395252190Srpaulo       The "-t file" or "--itbl file" option extends the basic set of
396252190Srpaulo       valid instructions by reading "file", a text file containing a
397252190Srpaulo       list of instruction formats.  The additional opcodes and their
398252190Srpaulo       formats are added to the built-in set of instructions, and
399252190Srpaulo       mnemonics for new registers may also be defined.  */
400252190Srpaulo    {"itbl", required_argument, NULL, OPTION_INSTTBL},
401252190Srpaulo#define OPTION_LISTING_LHS_WIDTH (OPTION_STD_BASE + 9)
402252190Srpaulo    {"listing-lhs-width", required_argument, NULL, OPTION_LISTING_LHS_WIDTH},
403252190Srpaulo#define OPTION_LISTING_LHS_WIDTH2 (OPTION_STD_BASE + 10)
404252190Srpaulo    {"listing-lhs-width2", required_argument, NULL, OPTION_LISTING_LHS_WIDTH2},
405252190Srpaulo#define OPTION_LISTING_RHS_WIDTH (OPTION_STD_BASE + 11)
406252190Srpaulo    {"listing-rhs-width", required_argument, NULL, OPTION_LISTING_RHS_WIDTH},
407252190Srpaulo#define OPTION_LISTING_CONT_LINES (OPTION_STD_BASE + 12)
408252190Srpaulo    {"listing-cont-lines", required_argument, NULL, OPTION_LISTING_CONT_LINES},
409252190Srpaulo#define OPTION_DEPFILE (OPTION_STD_BASE + 13)
410252190Srpaulo    {"MD", required_argument, NULL, OPTION_DEPFILE},
411252190Srpaulo#define OPTION_GSTABS (OPTION_STD_BASE + 14)
412252190Srpaulo    {"gstabs", no_argument, NULL, OPTION_GSTABS},
413252190Srpaulo#define OPTION_STRIP_LOCAL_ABSOLUTE (OPTION_STD_BASE + 15)
414252190Srpaulo    {"strip-local-absolute", no_argument, NULL, OPTION_STRIP_LOCAL_ABSOLUTE},
415252190Srpaulo#define OPTION_TRADITIONAL_FORMAT (OPTION_STD_BASE + 16)
416252190Srpaulo    {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
417252190Srpaulo#define OPTION_GDWARF2 (OPTION_STD_BASE + 17)
418252190Srpaulo    {"gdwarf2", no_argument, NULL, OPTION_GDWARF2},
419252190Srpaulo    {"no-warn", no_argument, NULL, 'W'},
420252190Srpaulo#define OPTION_WARN (OPTION_STD_BASE + 18)
421252190Srpaulo    {"warn", no_argument, NULL, OPTION_WARN},
422252190Srpaulo#define OPTION_TARGET_HELP (OPTION_STD_BASE + 19)
423252190Srpaulo    {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
424252190Srpaulo#define OPTION_WARN_FATAL (OPTION_STD_BASE + 20)
425252190Srpaulo    {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL}
426252190Srpaulo    /* When you add options here, check that they do not collide with
427252190Srpaulo       OPTION_MD_BASE.  See as.h.  */
428252190Srpaulo  };
429252190Srpaulo
430252190Srpaulo  /* Construct the option lists from the standard list and the target
431252190Srpaulo     dependent list.  Include space for an extra NULL option and
432252190Srpaulo     always NULL terminate.  */
433252190Srpaulo  shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
434252190Srpaulo  longopts = (struct option *) xmalloc (sizeof (std_longopts)
435252190Srpaulo					+ md_longopts_size
436252190Srpaulo					+ sizeof (struct option));
437252190Srpaulo  memcpy (longopts, std_longopts, sizeof (std_longopts));
438252190Srpaulo  memcpy ((char *) longopts + sizeof (std_longopts),
439252190Srpaulo	  md_longopts, md_longopts_size);
440252190Srpaulo  memset ((char *) longopts + sizeof (std_longopts) + md_longopts_size,
441252190Srpaulo	  0, sizeof (struct option));
442252190Srpaulo
443252190Srpaulo  /* Make a local copy of the old argv.  */
444252190Srpaulo  old_argc = *pargc;
445252190Srpaulo  old_argv = *pargv;
446252190Srpaulo
447252190Srpaulo  /* Initialize a new argv that contains no options.  */
448252190Srpaulo  new_argv = (char **) xmalloc (sizeof (char *) * (old_argc + 1));
449252190Srpaulo  new_argv[0] = old_argv[0];
450252190Srpaulo  new_argc = 1;
451252190Srpaulo  new_argv[new_argc] = NULL;
452252190Srpaulo
453252190Srpaulo  while (1)
454252190Srpaulo    {
455252190Srpaulo      /* getopt_long_only is like getopt_long, but '-' as well as '--' can
456252190Srpaulo	 indicate a long option.  */
457252190Srpaulo      int longind;
458252190Srpaulo      int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
459252190Srpaulo				   &longind);
460252190Srpaulo
461252190Srpaulo      if (optc == -1)
462252190Srpaulo	break;
463252190Srpaulo
464252190Srpaulo      switch (optc)
465252190Srpaulo	{
466252190Srpaulo	default:
467252190Srpaulo	  /* md_parse_option should return 1 if it recognizes optc,
468252190Srpaulo	     0 if not.  */
469252190Srpaulo	  if (md_parse_option (optc, optarg) != 0)
470252190Srpaulo	    break;
471252190Srpaulo	  /* `-v' isn't included in the general short_opts list, so check for
472252190Srpaulo	     it explicity here before deciding we've gotten a bad argument.  */
473252190Srpaulo	  if (optc == 'v')
474252190Srpaulo	    {
475252190Srpaulo#ifdef VMS
476252190Srpaulo	      /* Telling getopt to treat -v's value as optional can result
477252190Srpaulo		 in it picking up a following filename argument here.  The
478252190Srpaulo		 VMS code in md_parse_option can return 0 in that case,
479252190Srpaulo		 but it has no way of pushing the filename argument back.  */
480252190Srpaulo	      if (optarg && *optarg)
481252190Srpaulo		new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
482252190Srpaulo	      else
483252190Srpaulo#else
484252190Srpaulo	      case 'v':
485252190Srpaulo#endif
486252190Srpaulo	      case OPTION_VERBOSE:
487252190Srpaulo		print_version_id ();
488252190Srpaulo	      break;
489252190Srpaulo	    }
490252190Srpaulo	  /* Fall through.  */
491252190Srpaulo
492252190Srpaulo	case '?':
493252190Srpaulo	  exit (EXIT_FAILURE);
494252190Srpaulo
495252190Srpaulo	case 1:			/* File name.  */
496252190Srpaulo	  if (!strcmp (optarg, "-"))
497252190Srpaulo	    optarg = "";
498252190Srpaulo	  new_argv[new_argc++] = optarg;
499252190Srpaulo	  new_argv[new_argc] = NULL;
500252190Srpaulo	  break;
501252190Srpaulo
502252190Srpaulo	case OPTION_TARGET_HELP:
503252190Srpaulo          md_show_usage (stdout);
504252190Srpaulo          exit (EXIT_SUCCESS);
505252190Srpaulo
506252190Srpaulo	case OPTION_HELP:
507252190Srpaulo	  show_usage (stdout);
508252190Srpaulo	  exit (EXIT_SUCCESS);
509252190Srpaulo
510252190Srpaulo	case OPTION_NOCPP:
511252190Srpaulo	  break;
512252190Srpaulo
513252190Srpaulo	case OPTION_STATISTICS:
514252190Srpaulo	  flag_print_statistics = 1;
515252190Srpaulo	  break;
516252190Srpaulo
517252190Srpaulo	case OPTION_STRIP_LOCAL_ABSOLUTE:
518252190Srpaulo	  flag_strip_local_absolute = 1;
519252190Srpaulo	  break;
520252190Srpaulo
521252190Srpaulo	case OPTION_TRADITIONAL_FORMAT:
522252190Srpaulo	  flag_traditional_format = 1;
523252190Srpaulo	  break;
524252190Srpaulo
525252190Srpaulo	case OPTION_VERSION:
526252190Srpaulo	  /* This output is intended to follow the GNU standards document.  */
527252190Srpaulo	  printf (_("GNU assembler %s\n"), BFD_VERSION_STRING);
528337817Scy	  printf (_("Copyright 2002 Free Software Foundation, Inc.\n"));
529337817Scy	  printf (_("\
530337817ScyThis program is free software; you may redistribute it under the terms of\n\
531337817Scythe GNU General Public License.  This program has absolutely no warranty.\n"));
532337817Scy	  printf (_("This assembler was configured for a target of `%s'.\n"),
533337817Scy		  TARGET_ALIAS);
534337817Scy	  exit (EXIT_SUCCESS);
535337817Scy
536337817Scy	case OPTION_EMULATION:
537337817Scy#ifdef USE_EMULATIONS
538337817Scy	  if (strcmp (optarg, this_emulation->name))
539337817Scy	    as_fatal (_("multiple emulation names specified"));
540337817Scy#else
541337817Scy	  as_fatal (_("emulations not handled in this configuration"));
542337817Scy#endif
543337817Scy	  break;
544337817Scy
545337817Scy	case OPTION_DUMPCONFIG:
546337817Scy	  fprintf (stderr, _("alias = %s\n"), TARGET_ALIAS);
547337817Scy	  fprintf (stderr, _("canonical = %s\n"), TARGET_CANONICAL);
548337817Scy	  fprintf (stderr, _("cpu-type = %s\n"), TARGET_CPU);
549337817Scy#ifdef TARGET_OBJ_FORMAT
550337817Scy	  fprintf (stderr, _("format = %s\n"), TARGET_OBJ_FORMAT);
551337817Scy#endif
552337817Scy#ifdef TARGET_FORMAT
553337817Scy	  fprintf (stderr, _("bfd-target = %s\n"), TARGET_FORMAT);
554337817Scy#endif
555337817Scy	  exit (EXIT_SUCCESS);
556337817Scy
557337817Scy	case OPTION_DEFSYM:
558337817Scy	  {
559337817Scy	    char *s;
560337817Scy	    valueT i;
561337817Scy	    struct defsym_list *n;
562337817Scy
563337817Scy	    for (s = optarg; *s != '\0' && *s != '='; s++)
564337817Scy	      ;
565337817Scy	    if (*s == '\0')
566337817Scy	      as_fatal (_("bad defsym; format is --defsym name=value"));
567337817Scy	    *s++ = '\0';
568337817Scy#ifdef BFD_ASSEMBLER
569337817Scy	    i = bfd_scan_vma (s, (const char **) NULL, 0);
570337817Scy#else
571337817Scy	    i = strtol (s, (char **) NULL, 0);
572337817Scy#endif
573337817Scy	    n = (struct defsym_list *) xmalloc (sizeof *n);
574337817Scy	    n->next = defsyms;
575337817Scy	    n->name = optarg;
576337817Scy	    n->value = i;
577337817Scy	    defsyms = n;
578337817Scy	  }
579337817Scy	  break;
580337817Scy
581337817Scy	case OPTION_INSTTBL:
582337817Scy	case 't':
583337817Scy	  {
584337817Scy	    /* optarg is the name of the file containing the instruction
585337817Scy	       formats, opcodes, register names, etc.  */
586337817Scy	    struct itbl_file_list *n;
587
588	    if (optarg == NULL)
589	      {
590		as_warn (_("no file name following -t option"));
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	      as_fatal (_("failed to read instruction table %s\n"),
606			itbl_files->name);
607	  }
608	  break;
609
610	case OPTION_DEPFILE:
611	  start_dependencies (optarg);
612	  break;
613
614	case OPTION_GSTABS:
615	  debug_type = DEBUG_STABS;
616	  break;
617
618	case OPTION_GDWARF2:
619	  debug_type = DEBUG_DWARF2;
620	  break;
621
622	case 'J':
623	  flag_signed_overflow_ok = 1;
624	  break;
625
626#ifndef WORKING_DOT_WORD
627	case 'K':
628	  flag_warn_displacement = 1;
629	  break;
630#endif
631
632	case 'L':
633	  flag_keep_locals = 1;
634	  break;
635
636	case OPTION_LISTING_LHS_WIDTH:
637	  listing_lhs_width = atoi (optarg);
638	  if (listing_lhs_width_second < listing_lhs_width)
639	    listing_lhs_width_second = listing_lhs_width;
640	  break;
641	case OPTION_LISTING_LHS_WIDTH2:
642	  {
643	    int tmp = atoi (optarg);
644	    if (tmp > listing_lhs_width)
645	      listing_lhs_width_second = tmp;
646	  }
647	  break;
648	case OPTION_LISTING_RHS_WIDTH:
649	  listing_rhs_width = atoi (optarg);
650	  break;
651	case OPTION_LISTING_CONT_LINES:
652	  listing_lhs_cont_lines = atoi (optarg);
653	  break;
654
655	case 'M':
656	  flag_mri = 1;
657#ifdef TC_M68K
658	  flag_m68k_mri = 1;
659#endif
660	  break;
661
662	case 'R':
663	  flag_readonly_data_in_text = 1;
664	  break;
665
666	case 'W':
667	  flag_no_warnings = 1;
668	  break;
669
670	case OPTION_WARN:
671	  flag_no_warnings = 0;
672	  flag_fatal_warnings = 0;
673	  break;
674
675	case OPTION_WARN_FATAL:
676	  flag_no_warnings = 0;
677	  flag_fatal_warnings = 1;
678	  break;
679
680	case 'Z':
681	  flag_always_generate_output = 1;
682	  break;
683
684	case 'a':
685	  if (optarg)
686	    {
687	      if (md_parse_option (optc, optarg) != 0)
688		break;
689
690	      while (*optarg)
691		{
692		  switch (*optarg)
693		    {
694		    case 'c':
695		      listing |= LISTING_NOCOND;
696		      break;
697		    case 'd':
698		      listing |= LISTING_NODEBUG;
699		      break;
700		    case 'h':
701		      listing |= LISTING_HLL;
702		      break;
703		    case 'l':
704		      listing |= LISTING_LISTING;
705		      break;
706		    case 'm':
707		      listing |= LISTING_MACEXP;
708		      break;
709		    case 'n':
710		      listing |= LISTING_NOFORM;
711		      break;
712		    case 's':
713		      listing |= LISTING_SYMBOLS;
714		      break;
715		    case '=':
716		      listing_filename = xstrdup (optarg + 1);
717		      optarg += strlen (listing_filename);
718		      break;
719		    default:
720		      as_fatal (_("invalid listing option `%c'"), *optarg);
721		      break;
722		    }
723		  optarg++;
724		}
725	    }
726	  if (!listing)
727	    listing = LISTING_DEFAULT;
728	  break;
729
730	case 'D':
731	  /* DEBUG is implemented: it debugs different
732	     things from other people's assemblers.  */
733	  flag_debug = 1;
734	  break;
735
736	case 'f':
737	  flag_no_comments = 1;
738	  break;
739
740	case 'I':
741	  {			/* Include file directory.  */
742	    char *temp = xstrdup (optarg);
743	    add_include_dir (temp);
744	    break;
745	  }
746
747	case 'o':
748	  out_file_name = xstrdup (optarg);
749	  break;
750
751	case 'w':
752	  break;
753
754	case 'X':
755	  /* -X means treat warnings as errors.  */
756	  break;
757	}
758    }
759
760  free (shortopts);
761  free (longopts);
762
763  *pargc = new_argc;
764  *pargv = new_argv;
765
766#ifdef md_after_parse_args
767  md_after_parse_args ();
768#endif
769}
770
771static long start_time;
772
773int main PARAMS ((int, char **));
774
775int
776main (argc, argv)
777     int argc;
778     char **argv;
779{
780  int macro_alternate;
781  int macro_strip_at;
782  int keep_it;
783
784  start_time = get_run_time ();
785
786#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
787  setlocale (LC_MESSAGES, "");
788#endif
789#if defined (HAVE_SETLOCALE)
790  setlocale (LC_CTYPE, "");
791#endif
792  bindtextdomain (PACKAGE, LOCALEDIR);
793  textdomain (PACKAGE);
794
795  if (debug_memory)
796    {
797#ifdef BFD_ASSEMBLER
798      extern long _bfd_chunksize;
799      _bfd_chunksize = 64;
800#endif
801      chunksize = 64;
802    }
803
804#ifdef HOST_SPECIAL_INIT
805  HOST_SPECIAL_INIT (argc, argv);
806#endif
807
808  myname = argv[0];
809  xmalloc_set_program_name (myname);
810
811  START_PROGRESS (myname, 0);
812
813#ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
814#define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
815#endif
816
817  out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
818
819  hex_init ();
820#ifdef BFD_ASSEMBLER
821  bfd_init ();
822  bfd_set_error_program_name (myname);
823#endif
824
825#ifdef USE_EMULATIONS
826  select_emulation_mode (argc, argv);
827#endif
828
829  PROGRESS (1);
830  symbol_begin ();
831  frag_init ();
832  subsegs_begin ();
833  parse_args (&argc, &argv);
834  read_begin ();
835  input_scrub_begin ();
836  expr_begin ();
837
838  if (flag_print_statistics)
839    xatexit (dump_statistics);
840
841  macro_alternate = 0;
842  macro_strip_at = 0;
843#ifdef TC_I960
844  macro_strip_at = flag_mri;
845#endif
846#ifdef TC_A29K
847  /* For compatibility with the AMD 29K family macro assembler
848     specification.  */
849  macro_alternate = 1;
850  macro_strip_at = 1;
851#endif
852
853  macro_init (macro_alternate, flag_mri, macro_strip_at, macro_expr);
854
855  PROGRESS (1);
856
857#ifdef BFD_ASSEMBLER
858  output_file_create (out_file_name);
859  assert (stdoutput != 0);
860#endif
861
862#ifdef tc_init_after_args
863  tc_init_after_args ();
864#endif
865
866  itbl_init ();
867
868  /* Now that we have fully initialized, and have created the output
869     file, define any symbols requested by --defsym command line
870     arguments.  */
871  while (defsyms != NULL)
872    {
873      symbolS *sym;
874      struct defsym_list *next;
875
876      sym = symbol_new (defsyms->name, absolute_section, defsyms->value,
877			&zero_address_frag);
878      symbol_table_insert (sym);
879      next = defsyms->next;
880      free (defsyms);
881      defsyms = next;
882    }
883
884  PROGRESS (1);
885
886  /* Assemble it.  */
887  perform_an_assembly_pass (argc, argv);
888
889  cond_finish_check (-1);
890
891#ifdef md_end
892  md_end ();
893#endif
894
895  /* If we've been collecting dwarf2 .debug_line info, either for
896     assembly debugging or on behalf of the compiler, emit it now.  */
897  dwarf2_finish ();
898
899  if (seen_at_least_1_file ()
900      && (flag_always_generate_output || had_errors () == 0))
901    keep_it = 1;
902  else
903    keep_it = 0;
904
905#if defined (BFD_ASSEMBLER) || !defined (BFD)
906  /* This used to be done at the start of write_object_file in
907     write.c, but that caused problems when doing listings when
908     keep_it was zero.  This could probably be moved above md_end, but
909     I didn't want to risk the change.  */
910  subsegs_finish ();
911#endif
912
913  if (keep_it)
914    write_object_file ();
915
916#ifndef NO_LISTING
917  listing_print (listing_filename);
918#endif
919
920#ifndef OBJ_VMS /* does its own file handling */
921#ifndef BFD_ASSEMBLER
922  if (keep_it)
923#endif
924    output_file_close (out_file_name);
925#endif
926
927  if (flag_fatal_warnings && had_warnings () > 0 && had_errors () == 0)
928    as_bad (_("%d warnings, treating warnings as errors"), had_warnings ());
929
930  if (had_errors () > 0 && ! flag_always_generate_output)
931    keep_it = 0;
932
933  if (!keep_it)
934    unlink (out_file_name);
935
936  input_scrub_end ();
937
938  END_PROGRESS (myname);
939
940  /* Use xexit instead of return, because under VMS environments they
941     may not place the same interpretation on the value given.  */
942  if (had_errors () > 0)
943    xexit (EXIT_FAILURE);
944
945  /* Only generate dependency file if assembler was successful.  */
946  print_dependencies ();
947
948  xexit (EXIT_SUCCESS);
949}
950
951static void
952dump_statistics ()
953{
954#ifdef HAVE_SBRK
955  char *lim = (char *) sbrk (0);
956#endif
957  long run_time = get_run_time () - start_time;
958
959  fprintf (stderr, _("%s: total time in assembly: %ld.%06ld\n"),
960	   myname, run_time / 1000000, run_time % 1000000);
961#ifdef HAVE_SBRK
962  fprintf (stderr, _("%s: data size %ld\n"),
963	   myname, (long) (lim - (char *) &environ));
964#endif
965
966  subsegs_print_statistics (stderr);
967  write_print_statistics (stderr);
968  symbol_print_statistics (stderr);
969  read_print_statistics (stderr);
970
971#ifdef tc_print_statistics
972  tc_print_statistics (stderr);
973#endif
974#ifdef obj_print_statistics
975  obj_print_statistics (stderr);
976#endif
977}
978
979/* Here to attempt 1 pass over each input file.
980   We scan argv[*] looking for filenames or exactly "" which is
981   shorthand for stdin. Any argv that is NULL is not a file-name.
982   We set need_pass_2 TRUE if, after this, we still have unresolved
983   expressions of the form (unknown value)+-(unknown value).
984
985   Note the un*x semantics: there is only 1 logical input file, but it
986   may be a catenation of many 'physical' input files.  */
987
988static void
989perform_an_assembly_pass (argc, argv)
990     int argc;
991     char **argv;
992{
993  int saw_a_file = 0;
994#ifdef BFD_ASSEMBLER
995  flagword applicable;
996#endif
997
998  need_pass_2 = 0;
999
1000#ifndef BFD_ASSEMBLER
1001#ifdef MANY_SEGMENTS
1002  {
1003    unsigned int i;
1004    for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1005      segment_info[i].fix_root = 0;
1006  }
1007  /* Create the three fixed ones.  */
1008  {
1009    segT seg;
1010
1011#ifdef TE_APOLLO
1012    seg = subseg_new (".wtext", 0);
1013#else
1014    seg = subseg_new (".text", 0);
1015#endif
1016    assert (seg == SEG_E0);
1017    seg = subseg_new (".data", 0);
1018    assert (seg == SEG_E1);
1019    seg = subseg_new (".bss", 0);
1020    assert (seg == SEG_E2);
1021#ifdef TE_APOLLO
1022    create_target_segments ();
1023#endif
1024  }
1025
1026#else /* not MANY_SEGMENTS */
1027  text_fix_root = NULL;
1028  data_fix_root = NULL;
1029  bss_fix_root = NULL;
1030#endif /* not MANY_SEGMENTS */
1031#else /* BFD_ASSEMBLER */
1032  /* Create the standard sections, and those the assembler uses
1033     internally.  */
1034  text_section = subseg_new (TEXT_SECTION_NAME, 0);
1035  data_section = subseg_new (DATA_SECTION_NAME, 0);
1036  bss_section = subseg_new (BSS_SECTION_NAME, 0);
1037  /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
1038     to have relocs, otherwise we don't find out in time.  */
1039  applicable = bfd_applicable_section_flags (stdoutput);
1040  bfd_set_section_flags (stdoutput, text_section,
1041			 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1042				       | SEC_CODE | SEC_READONLY));
1043  bfd_set_section_flags (stdoutput, data_section,
1044			 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1045				       | SEC_DATA));
1046  bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
1047  seg_info (bss_section)->bss = 1;
1048  subseg_new (BFD_ABS_SECTION_NAME, 0);
1049  subseg_new (BFD_UND_SECTION_NAME, 0);
1050  reg_section = subseg_new ("*GAS `reg' section*", 0);
1051  expr_section = subseg_new ("*GAS `expr' section*", 0);
1052
1053#endif /* BFD_ASSEMBLER */
1054
1055  subseg_set (text_section, 0);
1056
1057  /* This may add symbol table entries, which requires having an open BFD,
1058     and sections already created, in BFD_ASSEMBLER mode.  */
1059  md_begin ();
1060
1061#ifdef obj_begin
1062  obj_begin ();
1063#endif
1064
1065  /* Skip argv[0].  */
1066  argv++;
1067  argc--;
1068
1069  while (argc--)
1070    {
1071      if (*argv)
1072	{			/* Is it a file-name argument?  */
1073	  PROGRESS (1);
1074	  saw_a_file++;
1075	  /* argv->"" if stdin desired, else->filename  */
1076	  read_a_source_file (*argv);
1077	}
1078      argv++;			/* completed that argv  */
1079    }
1080  if (!saw_a_file)
1081    read_a_source_file ("");
1082}
1083
1084/* The interface between the macro code and gas expression handling.  */
1085
1086static int
1087macro_expr (emsg, idx, in, val)
1088     const char *emsg;
1089     int idx;
1090     sb *in;
1091     int *val;
1092{
1093  char *hold;
1094  expressionS ex;
1095
1096  sb_terminate (in);
1097
1098  hold = input_line_pointer;
1099  input_line_pointer = in->ptr + idx;
1100  expression (&ex);
1101  idx = input_line_pointer - in->ptr;
1102  input_line_pointer = hold;
1103
1104  if (ex.X_op != O_constant)
1105    as_bad ("%s", emsg);
1106
1107  *val = (int) ex.X_add_number;
1108
1109  return idx;
1110}
1111