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