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