* coffgen.c (coff_print_symbol): Cast pointer different to long
[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\
113 s include symbols\n\
baed44cd 114-D produce assembler debugging messages\n\
ebd6f117
DM
115-f skip whitespace and comment preprocessing\n\
116--help show this message and exit\n\
117-I DIR add DIR to search list for .include directives\n\
118-J don't warn about signed overflow\n\
119-K warn when differences altered for long displacements\n\
120-L keep local symbols (starting with `L')\n");
121 fprintf (stream, "\
baed44cd 122-nocpp ignored\n\
f3d817d8 123-o OBJFILE name the object-file output OBJFILE (default a.out)\n\
ebd6f117
DM
124-R fold data section into text section\n\
125--statistics print maximum bytes and total seconds used\n\
ebd6f117
DM
126--version print assembler version number and exit\n\
127-W suppress warnings\n\
128-w ignored\n\
baed44cd 129-X ignored\n\
ebd6f117
DM
130-Z generate object file even after errors\n");
131
f3d817d8 132 md_show_usage (stream);
ebd6f117
DM
133}
134
135/*
ebd6f117
DM
136 * Since it is easy to do here we interpret the special arg "-"
137 * to mean "use stdin" and we set that argv[] pointing to "".
138 * After we have munged argv[], the only things left are source file
139 * name(s) and ""(s) denoting stdin. These file names are used
140 * (perhaps more than once) later.
f3d817d8 141 *
ebd6f117
DM
142 * check for new machine-dep cmdline options in
143 * md_parse_option definitions in config/tc-*.c
144 */
145
146void
f3d817d8
DM
147parse_args (pargc, pargv)
148 int *pargc;
149 char ***pargv;
ebd6f117 150{
f3d817d8
DM
151 int old_argc, new_argc;
152 char **old_argv, **new_argv;
153
154 /* Starting the short option string with '-' is for programs that
155 expect options and other ARGV-elements in any order and that care about
156 the ordering of the two. We describe each non-option ARGV-element
157 as if it were the argument of an option with character code 1. */
158
159 char *shortopts;
160 extern CONST char *md_shortopts;
def66e24
DM
161 /* -v takes an argument on VMS, so we don't make it a generic option.
162 It gets recognized as an abbreviation of -version, anyway. */
163 CONST char *std_shortopts = "-JKLRWZfa::DI:o:wX";
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);
182 longopts = xmalloc (sizeof (std_longopts) + md_longopts_size);
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
f3d817d8 242 case 'J':
def66e24
DM
243 flag_signed_overflow_ok = 1;
244 break;
245
f3d817d8 246 case 'K':
def66e24
DM
247 flag_warn_displacement = 1;
248 break;
249
f3d817d8 250 case 'L':
def66e24
DM
251 flag_keep_locals = 1;
252 break;
253
f3d817d8 254 case 'R':
def66e24
DM
255 flag_readonly_data_in_text = 1;
256 break;
257
f3d817d8 258 case 'W':
def66e24
DM
259 flag_no_warnings = 1;
260 break;
261
f3d817d8 262 case 'Z':
def66e24 263 flag_always_generate_output = 1;
f3d817d8
DM
264 break;
265
266 case 'a':
267 if (optarg)
6efd877d 268 {
f3d817d8 269 while (*optarg)
d3038350 270 {
f3d817d8
DM
271 switch (*optarg)
272 {
273 case 'd':
274 listing |= LISTING_NODEBUG;
275 break;
276 case 'h':
277 listing |= LISTING_HLL;
278 break;
279 case 'l':
280 listing |= LISTING_LISTING;
281 break;
282 case 'n':
283 listing |= LISTING_NOFORM;
284 break;
285 case 's':
286 listing |= LISTING_SYMBOLS;
287 break;
288 default:
289 as_bad ("invalid listing option `%c'", *optarg);
290 exit (EXIT_FAILURE);
291 break;
292 }
293 optarg++;
d3038350 294 }
6efd877d 295 }
f3d817d8
DM
296 if (!listing)
297 listing = LISTING_DEFAULT;
298 break;
299
300 case 'D':
301 /* DEBUG is implemented: it debugs different */
baed44cd 302 /* things from other people's assemblers. */
def66e24
DM
303 flag_debug = 1;
304 break;
305
306 case 'f':
307 flag_no_comments = 1;
f3d817d8
DM
308 break;
309
310 case 'I':
311 { /* Include file directory */
312 char *temp = strdup (optarg);
313 if (!temp)
314 as_fatal ("virtual memory exhausted");
315 add_include_dir (temp);
316 break;
317 }
318
319 case 'o':
320 out_file_name = strdup (optarg);
321 if (!out_file_name)
322 as_fatal ("virtual memory exhausted");
323 break;
324
f3d817d8
DM
325 case 'w':
326 break;
327
328 case 'X':
329 /* -X means treat warnings as errors */
330 break;
6efd877d 331 }
6efd877d 332 }
f3d817d8
DM
333
334 free (shortopts);
335 free (longopts);
336
337 *pargc = new_argc;
338 *pargv = new_argv;
ebd6f117
DM
339}
340
341int
342main (argc, argv)
343 int argc;
344 char **argv;
345{
346 char a;
347 int keep_it;
348 long start_time = get_run_time ();
349
350#if 0 /* do we need any of this?? */
351 {
352 static const int sig[] = {SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
353
354 for (a = 0; sig[a] != 0; a++)
355 if (signal (sig[a], SIG_IGN) != SIG_IGN)
356 signal (sig[a], got_sig);
357 }
358#endif
359
360 myname = argv[0];
ebd6f117
DM
361#ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
362#define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
363#endif
364 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
365
366#ifdef BFD_ASSEMBLER
367 bfd_init ();
368#endif
369
370 symbol_begin ();
371 subsegs_begin ();
372 read_begin ();
373 input_scrub_begin ();
374 frag_init ();
f3d817d8 375 parse_args (&argc, &argv);
67f3dd71
KR
376
377#ifdef BFD_ASSEMBLER
378 output_file_create (out_file_name);
379 assert (stdoutput != 0);
fecd2382 380#endif
67f3dd71 381
ebd6f117
DM
382#ifdef tc_init_after_args
383 tc_init_after_args ();
384#endif
385
6efd877d 386 perform_an_assembly_pass (argc, argv); /* Assemble it. */
fecd2382 387#ifdef TC_I960
6efd877d 388 brtab_emit ();
fecd2382 389#endif
8b13fa4e 390
6efd877d 391 if (seen_at_least_1_file ()
def66e24 392 && !((had_warnings () && flag_always_generate_output)
6efd877d 393 || had_errors () > 0))
398527f2 394 keep_it = 1;
f5200318 395 else
398527f2
KR
396 keep_it = 0;
397
398 if (keep_it)
399 write_object_file ();
400
401#ifndef NO_LISTING
402 listing_print ("");
f5200318 403#endif
6efd877d 404
ebd6f117 405#ifndef OBJ_VMS /* does its own file handling */
398527f2
KR
406#ifndef BFD_ASSEMBLER
407 if (keep_it)
408#endif
409 output_file_close (out_file_name);
ebd6f117 410#endif
398527f2
KR
411
412 if (!keep_it)
413 unlink (out_file_name);
414
6efd877d 415 input_scrub_end ();
398527f2
KR
416#ifdef md_end
417 md_end ();
418#endif
6efd877d 419
ebd6f117 420 if (statistics_flag)
ca7bd557
SS
421 {
422 extern char **environ;
423 char *lim = (char *) sbrk (0);
424 long run_time = get_run_time () - start_time;
425
fe920573 426 fprintf (stderr, "%s: total time in assembly: %ld.%06ld\n",
ca7bd557
SS
427 myname, run_time / 1000000, run_time % 1000000);
428 fprintf (stderr, "%s: data size %ld\n",
429 myname, (long) (lim - (char *) &environ));
430 }
431
def66e24 432 if ((had_warnings () && flag_always_generate_output)
d3038350
KR
433 || had_errors () > 0)
434 return EXIT_FAILURE;
435 return EXIT_SUCCESS;
436}
fecd2382 437\f
6efd877d 438
fecd2382
RP
439/* perform_an_assembly_pass()
440 *
441 * Here to attempt 1 pass over each input file.
442 * We scan argv[*] looking for filenames or exactly "" which is
443 * shorthand for stdin. Any argv that is NULL is not a file-name.
444 * We set need_pass_2 TRUE if, after this, we still have unresolved
445 * expressions of the form (unknown value)+-(unknown value).
446 *
447 * Note the un*x semantics: there is only 1 logical input file, but it
448 * may be a catenation of many 'physical' input files.
449 */
6efd877d
KR
450static void
451perform_an_assembly_pass (argc, argv)
452 int argc;
453 char **argv;
fecd2382 454{
6efd877d 455 int saw_a_file = 0;
67f3dd71
KR
456#ifdef BFD_ASSEMBLER
457 flagword applicable;
458#endif
459
6efd877d
KR
460 need_pass_2 = 0;
461
67f3dd71 462#ifndef BFD_ASSEMBLER
5d9f0ecf 463#ifdef MANY_SEGMENTS
f5200318
KR
464 {
465 unsigned int i;
466 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
6efd877d 467 segment_info[i].fix_root = 0;
f5200318 468 }
6efd877d 469 /* Create the three fixed ones */
6aba9d29
JL
470 {
471 segT seg;
472
ebd6f117
DM
473#ifdef TE_APOLLO
474 seg = subseg_new (".wtext", 0);
475#else
6aba9d29 476 seg = subseg_new (".text", 0);
ebd6f117 477#endif
6aba9d29
JL
478 assert (seg == SEG_E0);
479 seg = subseg_new (".data", 0);
480 assert (seg == SEG_E1);
481 seg = subseg_new (".bss", 0);
482 assert (seg == SEG_E2);
ebd6f117
DM
483#ifdef TE_APOLLO
484 create_target_segments ();
485#endif
6aba9d29
JL
486 }
487
e6498b10 488#else /* not MANY_SEGMENTS */
6efd877d
KR
489 text_fix_root = NULL;
490 data_fix_root = NULL;
491 bss_fix_root = NULL;
e6498b10 492#endif /* not MANY_SEGMENTS */
67f3dd71
KR
493#else /* BFD_ASSEMBLER */
494 /* Create the standard sections, and those the assembler uses
495 internally. */
496 text_section = subseg_new (".text", 0);
67f3dd71 497 data_section = subseg_new (".data", 0);
67f3dd71 498 bss_section = subseg_new (".bss", 0);
67f3dd71
KR
499 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
500 to have relocs, otherwise we don't find out in time. */
501 applicable = bfd_applicable_section_flags (stdoutput);
502 bfd_set_section_flags (stdoutput, text_section,
503 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
504 | SEC_CODE | SEC_READONLY));
505 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
506 bfd_set_section_flags (stdoutput, data_section,
507 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC));
508 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
8b13fa4e 509 seg_info (bss_section)->bss = 1;
67f3dd71
KR
510 subseg_new (BFD_ABS_SECTION_NAME, 0);
511 subseg_new (BFD_UND_SECTION_NAME, 0);
67f3dd71 512 reg_section = subseg_new ("*GAS `reg' section*", 0);
6aba9d29 513 expr_section = subseg_new ("*GAS `expr' section*", 0);
67f3dd71 514
67f3dd71 515#endif /* BFD_ASSEMBLER */
e6498b10 516
6aba9d29
JL
517 subseg_set (text_section, 0);
518
f5200318
KR
519 /* This may add symbol table entries, which requires having an open BFD,
520 and sections already created, in BFD_ASSEMBLER mode. */
521 md_begin ();
522
6efd877d
KR
523 argv++; /* skip argv[0] */
524 argc--; /* skip argv[0] */
525 while (argc--)
526 {
527 if (*argv)
528 { /* Is it a file-name argument? */
529 saw_a_file++;
530 /* argv->"" if stdin desired, else->filename */
531 read_a_source_file (*argv);
a39116f1 532 }
6efd877d
KR
533 argv++; /* completed that argv */
534 }
535 if (!saw_a_file)
536 read_a_source_file ("");
537} /* perform_an_assembly_pass() */
fecd2382 538\f
6aba9d29
JL
539#if 0
540/* This is not currently used. */
fecd2382 541static SIGTY
6efd877d
KR
542got_sig (sig)
543 int sig;
fecd2382 544{
6efd877d
KR
545 static here_before = 0;
546
547 as_bad ("Interrupted by signal %d", sig);
548 if (here_before++)
d3038350 549 exit (EXIT_FAILURE);
f5200318 550#if 0 /* If SIGTY is void, this produces warnings. */
6efd877d 551 return ((SIGTY) 0);
f5200318 552#endif
fecd2382 553}
6aba9d29 554#endif
fecd2382 555
a39116f1 556/* end of as.c */
This page took 0.144001 seconds and 4 git commands to generate.