* hppa-tdep.c (frame_saved_pc): Don't try to dig a return pointer
[deliverable/binutils-gdb.git] / gas / as.c
1 /* as.c - GAS main program.
2 Copyright (C) 1987, 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 * Main program for AS; a 32-bit assembler of GNU.
22 * Understands command arguments.
23 * Has a few routines that don't fit in other modules because they
24 * are shared.
25 *
26 *
27 * bugs
28 *
29 * : initialisers
30 * Since no-one else says they will support them in future: I
31 * don't support them now.
32 *
33 */
34
35 #include "ansidecl.h"
36 #include "libiberty.h"
37
38 #define COMMON
39
40 #include "as.h"
41 #include "subsegs.h"
42 #include "output-file.h"
43
44 static void perform_an_assembly_pass PARAMS ((int argc, char **argv));
45
46 int listing; /* true if a listing is wanted */
47
48 static char *listing_filename = NULL; /* Name of listing file. */
49
50 char *myname; /* argv[0] */
51 #ifdef BFD_ASSEMBLER
52 segT reg_section, expr_section;
53 segT text_section, data_section, bss_section;
54 #endif
55
56 \f
57 void
58 print_version_id ()
59 {
60 static int printed;
61 if (printed)
62 return;
63 printed = 1;
64
65 fprintf (stderr, "GNU assembler version %s (%s)", GAS_VERSION, TARGET_ALIAS);
66 #ifdef BFD_ASSEMBLER
67 fprintf (stderr, ", using BFD version %s", BFD_VERSION);
68 #endif
69 fprintf (stderr, "\n");
70 }
71
72 void
73 show_usage (stream)
74 FILE *stream;
75 {
76 fprintf (stream, "Usage: %s [option...] [asmfile...]\n", myname);
77
78 fprintf (stream, "\
79 Options:\n\
80 -a[sub-option...] turn on listings\n\
81 Sub-options [default hls]:\n\
82 d omit debugging directives\n\
83 h include high-level source\n\
84 l include assembly\n\
85 n omit forms processing\n\
86 s include symbols\n\
87 =file set listing file name (must be last sub-option)\n");
88 fprintf (stream, "\
89 -D produce assembler debugging messages\n\
90 -f skip whitespace and comment preprocessing\n\
91 --help show this message and exit\n\
92 -I DIR add DIR to search list for .include directives\n\
93 -J don't warn about signed overflow\n\
94 -K warn when differences altered for long displacements\n\
95 -L keep local symbols (starting with `L')\n");
96 fprintf (stream, "\
97 -nocpp ignored\n\
98 -o OBJFILE name the object-file output OBJFILE (default a.out)\n\
99 -R fold data section into text section\n\
100 --statistics print maximum bytes and total seconds used\n\
101 --version print assembler version number and exit\n\
102 -W suppress warnings\n\
103 -w ignored\n\
104 -X ignored\n\
105 -Z generate object file even after errors\n");
106
107 md_show_usage (stream);
108 }
109
110 #ifdef USE_EMULATIONS
111 #define EMULATION_ENVIRON "AS_EMULATION"
112
113 extern struct emulation mipsbelf, mipslelf, mipself;
114 extern struct emulation mipsbecoff, mipslecoff, mipsecoff;
115
116 static const char *emulation_name;
117 static struct emulation *const emulations[] = { EMULATIONS };
118 static const int n_emulations = sizeof (emulations) / sizeof (emulations[0]);
119
120 static void
121 select_emulation_mode (argc, argv)
122 int argc;
123 char **argv;
124 {
125 int i;
126 char *p, *em = 0;
127
128 for (i = 1; i < argc; i++)
129 if (!strncmp ("--em", argv[i], 4))
130 break;
131
132 if (i == argc)
133 goto do_default;
134
135 p = strchr (argv[i], '=');
136 if (p)
137 p++;
138 else
139 p = argv[i+1];
140
141 if (!p || !*p)
142 as_fatal ("missing emulation mode name");
143 em = p;
144
145 do_default:
146 if (em == 0)
147 em = getenv (EMULATION_ENVIRON);
148 if (em == 0)
149 em = DEFAULT_EMULATION;
150
151 if (em)
152 {
153 for (i = 0; i < n_emulations; i++)
154 if (!strcmp (emulations[i]->name, em))
155 break;
156 if (i == n_emulations)
157 as_fatal ("unrecognized emulation name `%s'", em);
158 this_emulation = emulations[i];
159 }
160 else
161 this_emulation = emulations[0];
162
163 this_emulation->init ();
164 }
165
166 const char *
167 default_emul_bfd_name ()
168 {
169 abort ();
170 }
171
172 void
173 common_emul_init ()
174 {
175 this_format = this_emulation->format;
176
177 if (this_emulation->leading_underscore == 2)
178 this_emulation->leading_underscore = this_format->dfl_leading_underscore;
179
180 if (this_emulation->default_endian != 2)
181 target_big_endian = this_emulation->default_endian;
182
183 if (this_emulation->fake_label_name == 0)
184 {
185 if (this_emulation->leading_underscore)
186 this_emulation->fake_label_name = "L0\001";
187 else
188 /* What other parameters should we test? */
189 this_emulation->fake_label_name = ".L0\001";
190 }
191 }
192 #endif
193
194 /*
195 * Since it is easy to do here we interpret the special arg "-"
196 * to mean "use stdin" and we set that argv[] pointing to "".
197 * After we have munged argv[], the only things left are source file
198 * name(s) and ""(s) denoting stdin. These file names are used
199 * (perhaps more than once) later.
200 *
201 * check for new machine-dep cmdline options in
202 * md_parse_option definitions in config/tc-*.c
203 */
204
205 void
206 parse_args (pargc, pargv)
207 int *pargc;
208 char ***pargv;
209 {
210 int old_argc, new_argc;
211 char **old_argv, **new_argv;
212
213 /* Starting the short option string with '-' is for programs that
214 expect options and other ARGV-elements in any order and that care about
215 the ordering of the two. We describe each non-option ARGV-element
216 as if it were the argument of an option with character code 1. */
217
218 char *shortopts;
219 extern CONST char *md_shortopts;
220 static const char std_shortopts[] =
221 {
222 '-', 'J',
223 #ifndef WORKING_DOT_WORD
224 /* -K is not meaningful if .word is not being hacked. */
225 'K',
226 #endif
227 'L', 'R', 'W', 'Z', 'f', 'a', ':', ':', 'D', 'I', ':', 'o', ':',
228 #ifndef VMS
229 /* -v takes an argument on VMS, so we don't make it a generic
230 option. */
231 'v',
232 #endif
233 'w', 'X',
234 '\0'
235 };
236 struct option *longopts;
237 extern struct option md_longopts[];
238 extern size_t md_longopts_size;
239 static const struct option std_longopts[] = {
240 #define OPTION_HELP (OPTION_STD_BASE)
241 {"help", no_argument, NULL, OPTION_HELP},
242 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
243 {"nocpp", no_argument, NULL, OPTION_NOCPP},
244 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
245 {"statistics", no_argument, NULL, OPTION_STATISTICS},
246 #define OPTION_VERSION (OPTION_STD_BASE + 3)
247 {"version", no_argument, NULL, OPTION_VERSION},
248 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
249 {"dump-config", no_argument, NULL, OPTION_DUMPCONFIG},
250 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
251 {"verbose", no_argument, NULL, OPTION_VERBOSE},
252 #define OPTION_EMULATION (OPTION_STD_BASE + 6)
253 {"emulation", required_argument, NULL, OPTION_EMULATION},
254 };
255
256 /* Construct the option lists from the standard list and the
257 target dependent list. */
258 shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
259 longopts = (struct option *) xmalloc (sizeof (std_longopts) + md_longopts_size);
260 memcpy (longopts, std_longopts, sizeof (std_longopts));
261 memcpy ((char *) longopts + sizeof (std_longopts),
262 md_longopts, md_longopts_size);
263
264 /* Make a local copy of the old argv. */
265 old_argc = *pargc;
266 old_argv = *pargv;
267
268 /* Initialize a new argv that contains no options. */
269 new_argv = (char **) xmalloc (sizeof (char *) * (old_argc + 1));
270 new_argv[0] = old_argv[0];
271 new_argc = 1;
272 new_argv[new_argc] = NULL;
273
274 while (1)
275 {
276 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
277 indicate a long option. */
278 int longind;
279 int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
280 &longind);
281
282 if (optc == -1)
283 break;
284
285 switch (optc)
286 {
287 default:
288 /* md_parse_option should return 1 if it recognizes optc,
289 0 if not. */
290 if (md_parse_option (optc, optarg) != 0)
291 break;
292 /* `-v' isn't included in the general short_opts list, so check for
293 it explicity here before deciding we've gotten a bad argument. */
294 if (optc == 'v')
295 {
296 #ifdef VMS
297 /* Telling getopt to treat -v's value as optional can result
298 in it picking up a following filename argument here. The
299 VMS code in md_parse_option can return 0 in that case,
300 but it has no way of pushing the filename argument back. */
301 if (optarg && *optarg)
302 new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
303 else
304 #else
305 case 'v':
306 #endif
307 case OPTION_VERBOSE:
308 print_version_id ();
309 break;
310 }
311 /*FALLTHRU*/
312
313 case '?':
314 exit (EXIT_FAILURE);
315
316 case 1: /* File name. */
317 if (!strcmp (optarg, "-"))
318 optarg = "";
319 new_argv[new_argc++] = optarg;
320 new_argv[new_argc] = NULL;
321 break;
322
323 case OPTION_HELP:
324 show_usage (stdout);
325 exit (EXIT_SUCCESS);
326
327 case OPTION_NOCPP:
328 break;
329
330 case OPTION_STATISTICS:
331 flag_print_statistics = 1;
332 break;
333
334 case OPTION_VERSION:
335 print_version_id ();
336 exit (EXIT_SUCCESS);
337
338 case OPTION_EMULATION:
339 #ifdef USE_EMULATIONS
340 if (strcmp (optarg, this_emulation->name))
341 as_fatal ("multiple emulation names specified");
342 #else
343 as_fatal ("emulations not handled in this configuration");
344 #endif
345 break;
346
347 case OPTION_DUMPCONFIG:
348 fprintf (stderr, "alias = %s\n", TARGET_ALIAS);
349 fprintf (stderr, "canonical = %s\n", TARGET_CANONICAL);
350 fprintf (stderr, "cpu-type = %s\n", TARGET_CPU);
351 #ifdef TARGET_OBJ_FORMAT
352 fprintf (stderr, "format = %s\n", TARGET_OBJ_FORMAT);
353 #endif
354 #ifdef TARGET_FORMAT
355 fprintf (stderr, "bfd-target = %s\n", TARGET_FORMAT);
356 #endif
357 exit (EXIT_SUCCESS);
358
359 case 'J':
360 flag_signed_overflow_ok = 1;
361 break;
362
363 #ifndef WORKING_DOT_WORD
364 case 'K':
365 flag_warn_displacement = 1;
366 break;
367 #endif
368
369 case 'L':
370 flag_keep_locals = 1;
371 break;
372
373 case 'R':
374 flag_readonly_data_in_text = 1;
375 break;
376
377 case 'W':
378 flag_no_warnings = 1;
379 break;
380
381 case 'Z':
382 flag_always_generate_output = 1;
383 break;
384
385 case 'a':
386 if (optarg)
387 {
388 while (*optarg)
389 {
390 switch (*optarg)
391 {
392 case 'd':
393 listing |= LISTING_NODEBUG;
394 break;
395 case 'h':
396 listing |= LISTING_HLL;
397 break;
398 case 'l':
399 listing |= LISTING_LISTING;
400 break;
401 case 'n':
402 listing |= LISTING_NOFORM;
403 break;
404 case 's':
405 listing |= LISTING_SYMBOLS;
406 break;
407 case '=':
408 listing_filename = strdup (optarg + 1);
409 if (listing_filename == NULL)
410 as_fatal ("virtual memory exhausted");
411 optarg += strlen (listing_filename);
412 break;
413 default:
414 as_fatal ("invalid listing option `%c'", *optarg);
415 break;
416 }
417 optarg++;
418 }
419 }
420 if (!listing)
421 listing = LISTING_DEFAULT;
422 break;
423
424 case 'D':
425 /* DEBUG is implemented: it debugs different */
426 /* things from other people's assemblers. */
427 flag_debug = 1;
428 break;
429
430 case 'f':
431 flag_no_comments = 1;
432 break;
433
434 case 'I':
435 { /* Include file directory */
436 char *temp = strdup (optarg);
437 if (!temp)
438 as_fatal ("virtual memory exhausted");
439 add_include_dir (temp);
440 break;
441 }
442
443 case 'o':
444 out_file_name = strdup (optarg);
445 if (!out_file_name)
446 as_fatal ("virtual memory exhausted");
447 break;
448
449 case 'w':
450 break;
451
452 case 'X':
453 /* -X means treat warnings as errors */
454 break;
455 }
456 }
457
458 free (shortopts);
459 free (longopts);
460
461 *pargc = new_argc;
462 *pargv = new_argv;
463 }
464
465 int
466 main (argc, argv)
467 int argc;
468 char **argv;
469 {
470 int keep_it;
471 long start_time = get_run_time ();
472
473 #ifdef HOST_SPECIAL_INIT
474 HOST_SPECIAL_INIT (argc, argv);
475 #endif
476
477 myname = argv[0];
478 xmalloc_set_program_name (myname);
479
480 START_PROGRESS (myname, 0);
481
482 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
483 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
484 #endif
485
486 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
487
488 hex_init ();
489 #ifdef BFD_ASSEMBLER
490 bfd_init ();
491 #endif
492
493 #ifdef USE_EMULATIONS
494 select_emulation_mode (argc, argv);
495 #endif
496
497 PROGRESS (1);
498 symbol_begin ();
499 frag_init ();
500 subsegs_begin ();
501 read_begin ();
502 input_scrub_begin ();
503 PROGRESS (1);
504 parse_args (&argc, &argv);
505
506 PROGRESS (1);
507
508 #ifdef BFD_ASSEMBLER
509 output_file_create (out_file_name);
510 assert (stdoutput != 0);
511 #endif
512
513 #ifdef tc_init_after_args
514 tc_init_after_args ();
515 #endif
516
517 PROGRESS (1);
518
519 perform_an_assembly_pass (argc, argv); /* Assemble it. */
520 #ifdef TC_I960
521 brtab_emit ();
522 #endif
523 /* start-sanitize-rce */
524 #ifdef TC_RCE
525 dump_literals(0);
526 #endif
527 /* end-sanitize-rce */
528
529 if (seen_at_least_1_file ()
530 && !((had_warnings () && flag_always_generate_output)
531 || had_errors () > 0))
532 keep_it = 1;
533 else
534 keep_it = 0;
535
536 if (keep_it)
537 write_object_file ();
538
539 #ifndef NO_LISTING
540 listing_print (listing_filename);
541 #endif
542
543 #ifndef OBJ_VMS /* does its own file handling */
544 #ifndef BFD_ASSEMBLER
545 if (keep_it)
546 #endif
547 output_file_close (out_file_name);
548 #endif
549
550 if (!keep_it)
551 unlink (out_file_name);
552
553 input_scrub_end ();
554 #ifdef md_end
555 md_end ();
556 #endif
557
558 END_PROGRESS (myname);
559
560 if (flag_print_statistics)
561 {
562 extern char **environ;
563 #ifdef HAVE_SBRK
564 char *lim = (char *) sbrk (0);
565 #endif
566 long run_time = get_run_time () - start_time;
567
568 fprintf (stderr, "%s: total time in assembly: %ld.%06ld\n",
569 myname, run_time / 1000000, run_time % 1000000);
570 #ifdef HAVE_SBRK
571 fprintf (stderr, "%s: data size %ld\n",
572 myname, (long) (lim - (char *) &environ));
573 #endif
574 }
575
576 /* Use exit instead of return, because under VMS environments they
577 may not place the same interpretation on the value given. */
578 if ((had_warnings () && flag_always_generate_output)
579 || had_errors () > 0)
580 exit (EXIT_FAILURE);
581 exit (EXIT_SUCCESS);
582 }
583 \f
584
585 /* perform_an_assembly_pass()
586 *
587 * Here to attempt 1 pass over each input file.
588 * We scan argv[*] looking for filenames or exactly "" which is
589 * shorthand for stdin. Any argv that is NULL is not a file-name.
590 * We set need_pass_2 TRUE if, after this, we still have unresolved
591 * expressions of the form (unknown value)+-(unknown value).
592 *
593 * Note the un*x semantics: there is only 1 logical input file, but it
594 * may be a catenation of many 'physical' input files.
595 */
596 static void
597 perform_an_assembly_pass (argc, argv)
598 int argc;
599 char **argv;
600 {
601 int saw_a_file = 0;
602 #ifdef BFD_ASSEMBLER
603 flagword applicable;
604 #endif
605
606 need_pass_2 = 0;
607
608 #ifndef BFD_ASSEMBLER
609 #ifdef MANY_SEGMENTS
610 {
611 unsigned int i;
612 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
613 segment_info[i].fix_root = 0;
614 }
615 /* Create the three fixed ones */
616 {
617 segT seg;
618
619 #ifdef TE_APOLLO
620 seg = subseg_new (".wtext", 0);
621 #else
622 seg = subseg_new (".text", 0);
623 #endif
624 assert (seg == SEG_E0);
625 seg = subseg_new (".data", 0);
626 assert (seg == SEG_E1);
627 seg = subseg_new (".bss", 0);
628 assert (seg == SEG_E2);
629 #ifdef TE_APOLLO
630 create_target_segments ();
631 #endif
632 }
633
634 #else /* not MANY_SEGMENTS */
635 text_fix_root = NULL;
636 data_fix_root = NULL;
637 bss_fix_root = NULL;
638 #endif /* not MANY_SEGMENTS */
639 #else /* BFD_ASSEMBLER */
640 /* Create the standard sections, and those the assembler uses
641 internally. */
642 text_section = subseg_new (".text", 0);
643 data_section = subseg_new (".data", 0);
644 bss_section = subseg_new (".bss", 0);
645 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
646 to have relocs, otherwise we don't find out in time. */
647 applicable = bfd_applicable_section_flags (stdoutput);
648 bfd_set_section_flags (stdoutput, text_section,
649 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
650 | SEC_CODE | SEC_READONLY));
651 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
652 bfd_set_section_flags (stdoutput, data_section,
653 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC));
654 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
655 seg_info (bss_section)->bss = 1;
656 subseg_new (BFD_ABS_SECTION_NAME, 0);
657 subseg_new (BFD_UND_SECTION_NAME, 0);
658 reg_section = subseg_new ("*GAS `reg' section*", 0);
659 expr_section = subseg_new ("*GAS `expr' section*", 0);
660
661 #endif /* BFD_ASSEMBLER */
662
663 subseg_set (text_section, 0);
664
665 /* This may add symbol table entries, which requires having an open BFD,
666 and sections already created, in BFD_ASSEMBLER mode. */
667 md_begin ();
668
669 argv++; /* skip argv[0] */
670 argc--; /* skip argv[0] */
671 while (argc--)
672 {
673 if (*argv)
674 { /* Is it a file-name argument? */
675 PROGRESS (1);
676 saw_a_file++;
677 /* argv->"" if stdin desired, else->filename */
678 read_a_source_file (*argv);
679 }
680 argv++; /* completed that argv */
681 }
682 if (!saw_a_file)
683 read_a_source_file ("");
684 } /* perform_an_assembly_pass() */
685
686 /* end of as.c */
This page took 0.041862 seconds and 4 git commands to generate.