source: branches/libc-0.6/src/binutils/gas/as.c

Last change on this file was 2512, checked in by bird, 20 years ago

Bug 20, fixed: use libiberty make_temp_file instead of big, clumsy and unsafe mktemp stuff.

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