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