* frags.c (frag_init): Call obstack_begin on `frags'.
[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 /*
108 * Since it is easy to do here we interpret the special arg "-"
109 * to mean "use stdin" and we set that argv[] pointing to "".
110 * After we have munged argv[], the only things left are source file
111 * name(s) and ""(s) denoting stdin. These file names are used
112 * (perhaps more than once) later.
113 *
114 * check for new machine-dep cmdline options in
115 * md_parse_option definitions in config/tc-*.c
116 */
117
118 void
119 parse_args (pargc, pargv)
120 int *pargc;
121 char ***pargv;
122 {
123 int old_argc, new_argc;
124 char **old_argv, **new_argv;
125
126 /* Starting the short option string with '-' is for programs that
127 expect options and other ARGV-elements in any order and that care about
128 the ordering of the two. We describe each non-option ARGV-element
129 as if it were the argument of an option with character code 1. */
130
131 char *shortopts;
132 extern CONST char *md_shortopts;
133 #ifdef VMS
134 /* -v takes an argument on VMS, so we don't make it a generic option. */
135 CONST char *std_shortopts = "-JKLRWZfa::DI:o:wX";
136 #else
137 CONST char *std_shortopts = "-JKLRWZfa::DI:o:vwX";
138 #endif
139
140 struct option *longopts;
141 extern struct option md_longopts[];
142 extern size_t md_longopts_size;
143 static const struct option std_longopts[] = {
144 #define OPTION_HELP (OPTION_STD_BASE)
145 {"help", no_argument, NULL, OPTION_HELP},
146 #define OPTION_NOCPP (OPTION_STD_BASE + 1)
147 {"nocpp", no_argument, NULL, OPTION_NOCPP},
148 #define OPTION_STATISTICS (OPTION_STD_BASE + 2)
149 {"statistics", no_argument, NULL, OPTION_STATISTICS},
150 #define OPTION_VERSION (OPTION_STD_BASE + 3)
151 {"version", no_argument, NULL, OPTION_VERSION},
152 #define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
153 {"dump-config", no_argument, NULL, OPTION_DUMPCONFIG},
154 #define OPTION_VERBOSE (OPTION_STD_BASE + 5)
155 {"verbose", no_argument, NULL, OPTION_VERBOSE},
156 };
157
158 /* Construct the option lists from the standard list and the
159 target dependent list. */
160 shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
161 longopts = (struct option *) xmalloc (sizeof (std_longopts) + md_longopts_size);
162 memcpy (longopts, std_longopts, sizeof (std_longopts));
163 memcpy ((char *) longopts + sizeof (std_longopts),
164 md_longopts, md_longopts_size);
165
166 /* Make a local copy of the old argv. */
167 old_argc = *pargc;
168 old_argv = *pargv;
169
170 /* Initialize a new argv that contains no options. */
171 new_argv = (char **) xmalloc (sizeof (char *) * (old_argc + 1));
172 new_argv[0] = old_argv[0];
173 new_argc = 1;
174 new_argv[new_argc] = NULL;
175
176 while (1)
177 {
178 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
179 indicate a long option. */
180 int longind;
181 int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
182 &longind);
183
184 if (optc == -1)
185 break;
186
187 switch (optc)
188 {
189 default:
190 /* md_parse_option should return 1 if it recognizes optc,
191 0 if not. */
192 if (md_parse_option (optc, optarg) != 0)
193 break;
194 /* `-v' isn't included in the general short_opts list, so check for
195 it explicity here before deciding we've gotten a bad argument. */
196 if (optc == 'v')
197 {
198 #ifdef VMS
199 /* Telling getopt to treat -v's value as optional can result
200 in it picking up a following filename argument here. The
201 VMS code in md_parse_option can return 0 in that case,
202 but it has no way of pushing the filename argument back. */
203 if (optarg && *optarg)
204 new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
205 else
206 #else
207 case 'v':
208 #endif
209 case OPTION_VERBOSE:
210 print_version_id ();
211 break;
212 }
213 /*FALLTHRU*/
214
215 case '?':
216 exit (EXIT_FAILURE);
217
218 case 1: /* File name. */
219 if (!strcmp (optarg, "-"))
220 optarg = "";
221 new_argv[new_argc++] = optarg;
222 new_argv[new_argc] = NULL;
223 break;
224
225 case OPTION_HELP:
226 show_usage (stdout);
227 exit (EXIT_SUCCESS);
228
229 case OPTION_NOCPP:
230 break;
231
232 case OPTION_STATISTICS:
233 flag_print_statistics = 1;
234 break;
235
236 case OPTION_VERSION:
237 print_version_id ();
238 exit (EXIT_SUCCESS);
239
240 case OPTION_DUMPCONFIG:
241 fprintf (stderr, "alias = %s\n", TARGET_ALIAS);
242 fprintf (stderr, "canonical = %s\n", TARGET_CANONICAL);
243 fprintf (stderr, "cpu-type = %s\n", TARGET_CPU);
244 #ifdef TARGET_OBJ_FORMAT
245 fprintf (stderr, "format = %s\n", TARGET_OBJ_FORMAT);
246 #endif
247 #ifdef TARGET_FORMAT
248 fprintf (stderr, "bfd-target = %s\n", TARGET_FORMAT);
249 #endif
250 exit (EXIT_SUCCESS);
251
252 case 'J':
253 flag_signed_overflow_ok = 1;
254 break;
255
256 case 'K':
257 flag_warn_displacement = 1;
258 break;
259
260 case 'L':
261 flag_keep_locals = 1;
262 break;
263
264 case 'R':
265 flag_readonly_data_in_text = 1;
266 break;
267
268 case 'W':
269 flag_no_warnings = 1;
270 break;
271
272 case 'Z':
273 flag_always_generate_output = 1;
274 break;
275
276 case 'a':
277 if (optarg)
278 {
279 while (*optarg)
280 {
281 switch (*optarg)
282 {
283 case 'd':
284 listing |= LISTING_NODEBUG;
285 break;
286 case 'h':
287 listing |= LISTING_HLL;
288 break;
289 case 'l':
290 listing |= LISTING_LISTING;
291 break;
292 case 'n':
293 listing |= LISTING_NOFORM;
294 break;
295 case 's':
296 listing |= LISTING_SYMBOLS;
297 break;
298 default:
299 as_fatal ("invalid listing option `%c'", *optarg);
300 break;
301 }
302 optarg++;
303 }
304 }
305 if (!listing)
306 listing = LISTING_DEFAULT;
307 break;
308
309 case 'D':
310 /* DEBUG is implemented: it debugs different */
311 /* things from other people's assemblers. */
312 flag_debug = 1;
313 break;
314
315 case 'f':
316 flag_no_comments = 1;
317 break;
318
319 case 'I':
320 { /* Include file directory */
321 char *temp = strdup (optarg);
322 if (!temp)
323 as_fatal ("virtual memory exhausted");
324 add_include_dir (temp);
325 break;
326 }
327
328 case 'o':
329 out_file_name = strdup (optarg);
330 if (!out_file_name)
331 as_fatal ("virtual memory exhausted");
332 break;
333
334 case 'w':
335 break;
336
337 case 'X':
338 /* -X means treat warnings as errors */
339 break;
340 }
341 }
342
343 free (shortopts);
344 free (longopts);
345
346 *pargc = new_argc;
347 *pargv = new_argv;
348 }
349
350 int
351 main (argc, argv)
352 int argc;
353 char **argv;
354 {
355 int keep_it;
356 long start_time = get_run_time ();
357
358 #ifdef HOST_SPECIAL_INIT
359 HOST_SPECIAL_INIT (argc, argv);
360 #endif
361
362 myname = argv[0];
363 xmalloc_set_program_name (myname);
364
365 START_PROGRESS (myname, 0);
366
367 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
368 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
369 #endif
370
371 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
372
373 #ifdef BFD_ASSEMBLER
374 bfd_init ();
375 #endif
376
377 PROGRESS (1);
378 symbol_begin ();
379 frag_init ();
380 subsegs_begin ();
381 read_begin ();
382 input_scrub_begin ();
383 PROGRESS (1);
384 parse_args (&argc, &argv);
385
386 PROGRESS (1);
387
388 #ifdef BFD_ASSEMBLER
389 output_file_create (out_file_name);
390 assert (stdoutput != 0);
391 #endif
392
393 #ifdef tc_init_after_args
394 tc_init_after_args ();
395 #endif
396
397 PROGRESS (1);
398
399 perform_an_assembly_pass (argc, argv); /* Assemble it. */
400 #ifdef TC_I960
401 brtab_emit ();
402 #endif
403 /* start-sanitize-rce */
404 #ifdef TC_RCE
405 dump_literals(0);
406 #endif
407 /* end-sanitize-rce */
408
409 if (seen_at_least_1_file ()
410 && !((had_warnings () && flag_always_generate_output)
411 || had_errors () > 0))
412 keep_it = 1;
413 else
414 keep_it = 0;
415
416 if (keep_it)
417 write_object_file ();
418
419 #ifndef NO_LISTING
420 listing_print ("");
421 #endif
422
423 #ifndef OBJ_VMS /* does its own file handling */
424 #ifndef BFD_ASSEMBLER
425 if (keep_it)
426 #endif
427 output_file_close (out_file_name);
428 #endif
429
430 if (!keep_it)
431 unlink (out_file_name);
432
433 input_scrub_end ();
434 #ifdef md_end
435 md_end ();
436 #endif
437
438 END_PROGRESS (myname);
439
440 if (flag_print_statistics)
441 {
442 extern char **environ;
443 char *lim = (char *) sbrk (0);
444 long run_time = get_run_time () - start_time;
445
446 fprintf (stderr, "%s: total time in assembly: %ld.%06ld\n",
447 myname, run_time / 1000000, run_time % 1000000);
448 fprintf (stderr, "%s: data size %ld\n",
449 myname, (long) (lim - (char *) &environ));
450 }
451
452 /* Use exit instead of return, because under VMS environments they
453 may not place the same interpretation on the value given. */
454 if ((had_warnings () && flag_always_generate_output)
455 || had_errors () > 0)
456 exit (EXIT_FAILURE);
457 exit (EXIT_SUCCESS);
458 }
459 \f
460
461 /* perform_an_assembly_pass()
462 *
463 * Here to attempt 1 pass over each input file.
464 * We scan argv[*] looking for filenames or exactly "" which is
465 * shorthand for stdin. Any argv that is NULL is not a file-name.
466 * We set need_pass_2 TRUE if, after this, we still have unresolved
467 * expressions of the form (unknown value)+-(unknown value).
468 *
469 * Note the un*x semantics: there is only 1 logical input file, but it
470 * may be a catenation of many 'physical' input files.
471 */
472 static void
473 perform_an_assembly_pass (argc, argv)
474 int argc;
475 char **argv;
476 {
477 int saw_a_file = 0;
478 #ifdef BFD_ASSEMBLER
479 flagword applicable;
480 #endif
481
482 need_pass_2 = 0;
483
484 #ifndef BFD_ASSEMBLER
485 #ifdef MANY_SEGMENTS
486 {
487 unsigned int i;
488 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
489 segment_info[i].fix_root = 0;
490 }
491 /* Create the three fixed ones */
492 {
493 segT seg;
494
495 #ifdef TE_APOLLO
496 seg = subseg_new (".wtext", 0);
497 #else
498 seg = subseg_new (".text", 0);
499 #endif
500 assert (seg == SEG_E0);
501 seg = subseg_new (".data", 0);
502 assert (seg == SEG_E1);
503 seg = subseg_new (".bss", 0);
504 assert (seg == SEG_E2);
505 #ifdef TE_APOLLO
506 create_target_segments ();
507 #endif
508 }
509
510 #else /* not MANY_SEGMENTS */
511 text_fix_root = NULL;
512 data_fix_root = NULL;
513 bss_fix_root = NULL;
514 #endif /* not MANY_SEGMENTS */
515 #else /* BFD_ASSEMBLER */
516 /* Create the standard sections, and those the assembler uses
517 internally. */
518 text_section = subseg_new (".text", 0);
519 data_section = subseg_new (".data", 0);
520 bss_section = subseg_new (".bss", 0);
521 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
522 to have relocs, otherwise we don't find out in time. */
523 applicable = bfd_applicable_section_flags (stdoutput);
524 bfd_set_section_flags (stdoutput, text_section,
525 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
526 | SEC_CODE | SEC_READONLY));
527 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
528 bfd_set_section_flags (stdoutput, data_section,
529 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC));
530 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
531 seg_info (bss_section)->bss = 1;
532 subseg_new (BFD_ABS_SECTION_NAME, 0);
533 subseg_new (BFD_UND_SECTION_NAME, 0);
534 reg_section = subseg_new ("*GAS `reg' section*", 0);
535 expr_section = subseg_new ("*GAS `expr' section*", 0);
536
537 #endif /* BFD_ASSEMBLER */
538
539 subseg_set (text_section, 0);
540
541 /* This may add symbol table entries, which requires having an open BFD,
542 and sections already created, in BFD_ASSEMBLER mode. */
543 md_begin ();
544
545 argv++; /* skip argv[0] */
546 argc--; /* skip argv[0] */
547 while (argc--)
548 {
549 if (*argv)
550 { /* Is it a file-name argument? */
551 PROGRESS (1);
552 saw_a_file++;
553 /* argv->"" if stdin desired, else->filename */
554 read_a_source_file (*argv);
555 }
556 argv++; /* completed that argv */
557 }
558 if (!saw_a_file)
559 read_a_source_file ("");
560 } /* perform_an_assembly_pass() */
561
562 /* end of as.c */