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