Gas reloc handling reeks.
[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
f5200318 44static void perform_an_assembly_pass PARAMS ((int argc, char **argv));
fecd2382 45
6efd877d 46int listing; /* true if a listing is wanted */
5d9f0ecf 47
6efd877d 48char *myname; /* argv[0] */
67f3dd71 49#ifdef BFD_ASSEMBLER
6aba9d29 50segT reg_section, expr_section;
67f3dd71
KR
51segT text_section, data_section, bss_section;
52#endif
ca7bd557 53
fecd2382 54\f
f5200318
KR
55void
56print_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
ebd6f117
DM
70void
71show_usage (stream)
72 FILE *stream;
fecd2382 73{
ebd6f117
DM
74 fprintf (stream, "Usage: %s [option...] [asmfile...]\n", myname);
75
76 fprintf (stream, "\
77Options:\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\
a2a1a548
ILT
84 s include symbols\n");
85 fprintf (stream, "\
baed44cd 86-D produce assembler debugging messages\n\
ebd6f117
DM
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, "\
baed44cd 94-nocpp ignored\n\
f3d817d8 95-o OBJFILE name the object-file output OBJFILE (default a.out)\n\
ebd6f117
DM
96-R fold data section into text section\n\
97--statistics print maximum bytes and total seconds used\n\
ebd6f117
DM
98--version print assembler version number and exit\n\
99-W suppress warnings\n\
100-w ignored\n\
baed44cd 101-X ignored\n\
ebd6f117
DM
102-Z generate object file even after errors\n");
103
f3d817d8 104 md_show_usage (stream);
ebd6f117
DM
105}
106
107/*
ebd6f117
DM
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.
f3d817d8 113 *
ebd6f117
DM
114 * check for new machine-dep cmdline options in
115 * md_parse_option definitions in config/tc-*.c
116 */
117
118void
f3d817d8
DM
119parse_args (pargc, pargv)
120 int *pargc;
121 char ***pargv;
ebd6f117 122{
f3d817d8
DM
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;
fb870b50
MT
133#ifdef VMS
134 /* -v takes an argument on VMS, so we don't make it a generic option. */
def66e24 135 CONST char *std_shortopts = "-JKLRWZfa::DI:o:wX";
a2a1a548 136#else
a2a1a548
ILT
137 CONST char *std_shortopts = "-JKLRWZfa::DI:o:vwX";
138#endif
f3d817d8
DM
139
140 struct option *longopts;
141 extern struct option md_longopts[];
142 extern size_t md_longopts_size;
460531da 143 static const struct option std_longopts[] = {
f3d817d8
DM
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},
460531da
KR
152#define OPTION_DUMPCONFIG (OPTION_STD_BASE + 4)
153 {"dump-config", no_argument, NULL, OPTION_DUMPCONFIG},
fb870b50
MT
154#define OPTION_VERBOSE (OPTION_STD_BASE + 5)
155 {"verbose", no_argument, NULL, OPTION_VERBOSE},
f3d817d8
DM
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);
a2a1a548 161 longopts = (struct option *) xmalloc (sizeof (std_longopts) + md_longopts_size);
f3d817d8
DM
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)
6efd877d 177 {
f3d817d8
DM
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);
ebd6f117 183
f3d817d8
DM
184 if (optc == -1)
185 break;
6efd877d 186
f3d817d8
DM
187 switch (optc)
188 {
189 default:
190 /* md_parse_option should return 1 if it recognizes optc,
191 0 if not. */
fb870b50
MT
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*/
f3d817d8
DM
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);
460531da 227 exit (EXIT_SUCCESS);
f3d817d8
DM
228
229 case OPTION_NOCPP:
230 break;
231
232 case OPTION_STATISTICS:
460531da 233 flag_print_statistics = 1;
f3d817d8
DM
234 break;
235
236 case OPTION_VERSION:
237 print_version_id ();
460531da
KR
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);
f3d817d8 251
f3d817d8 252 case 'J':
def66e24
DM
253 flag_signed_overflow_ok = 1;
254 break;
255
f3d817d8 256 case 'K':
def66e24
DM
257 flag_warn_displacement = 1;
258 break;
259
f3d817d8 260 case 'L':
def66e24
DM
261 flag_keep_locals = 1;
262 break;
263
f3d817d8 264 case 'R':
def66e24
DM
265 flag_readonly_data_in_text = 1;
266 break;
267
f3d817d8 268 case 'W':
def66e24
DM
269 flag_no_warnings = 1;
270 break;
271
f3d817d8 272 case 'Z':
def66e24 273 flag_always_generate_output = 1;
f3d817d8
DM
274 break;
275
276 case 'a':
277 if (optarg)
6efd877d 278 {
f3d817d8 279 while (*optarg)
d3038350 280 {
f3d817d8
DM
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:
460531da 299 as_fatal ("invalid listing option `%c'", *optarg);
f3d817d8
DM
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
460531da 361
ebd6f117 362 myname = argv[0];
d2a0c9f9
KR
363 xmalloc_set_program_name (myname);
364
87e48495
KR
365 START_PROGRESS (myname, 0);
366
ebd6f117
DM
367#ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
368#define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
369#endif
d2a0c9f9 370
ebd6f117
DM
371 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
372
373#ifdef BFD_ASSEMBLER
374 bfd_init ();
375#endif
376
87e48495 377 PROGRESS (1);
ebd6f117 378 symbol_begin ();
87e48495 379 frag_init ();
ebd6f117
DM
380 subsegs_begin ();
381 read_begin ();
382 input_scrub_begin ();
87e48495 383 PROGRESS (1);
f3d817d8 384 parse_args (&argc, &argv);
67f3dd71 385
87e48495
KR
386 PROGRESS (1);
387
67f3dd71
KR
388#ifdef BFD_ASSEMBLER
389 output_file_create (out_file_name);
390 assert (stdoutput != 0);
fecd2382 391#endif
67f3dd71 392
ebd6f117
DM
393#ifdef tc_init_after_args
394 tc_init_after_args ();
395#endif
396
87e48495
KR
397 PROGRESS (1);
398
6efd877d 399 perform_an_assembly_pass (argc, argv); /* Assemble it. */
fecd2382 400#ifdef TC_I960
6efd877d 401 brtab_emit ();
fecd2382 402#endif
fb870b50
MT
403/* start-sanitize-rce */
404#ifdef TC_RCE
405 dump_literals(0);
406#endif
407/* end-sanitize-rce */
8b13fa4e 408
6efd877d 409 if (seen_at_least_1_file ()
def66e24 410 && !((had_warnings () && flag_always_generate_output)
6efd877d 411 || had_errors () > 0))
398527f2 412 keep_it = 1;
f5200318 413 else
398527f2
KR
414 keep_it = 0;
415
416 if (keep_it)
417 write_object_file ();
418
419#ifndef NO_LISTING
420 listing_print ("");
f5200318 421#endif
6efd877d 422
ebd6f117 423#ifndef OBJ_VMS /* does its own file handling */
398527f2
KR
424#ifndef BFD_ASSEMBLER
425 if (keep_it)
426#endif
427 output_file_close (out_file_name);
ebd6f117 428#endif
398527f2
KR
429
430 if (!keep_it)
431 unlink (out_file_name);
432
6efd877d 433 input_scrub_end ();
398527f2
KR
434#ifdef md_end
435 md_end ();
436#endif
6efd877d 437
87e48495
KR
438 END_PROGRESS (myname);
439
460531da 440 if (flag_print_statistics)
ca7bd557
SS
441 {
442 extern char **environ;
443 char *lim = (char *) sbrk (0);
444 long run_time = get_run_time () - start_time;
445
fe920573 446 fprintf (stderr, "%s: total time in assembly: %ld.%06ld\n",
ca7bd557
SS
447 myname, run_time / 1000000, run_time % 1000000);
448 fprintf (stderr, "%s: data size %ld\n",
449 myname, (long) (lim - (char *) &environ));
450 }
451
460531da
KR
452 /* Use exit instead of return, because under VMS environments they
453 may not place the same interpretation on the value given. */
def66e24 454 if ((had_warnings () && flag_always_generate_output)
d3038350 455 || had_errors () > 0)
460531da
KR
456 exit (EXIT_FAILURE);
457 exit (EXIT_SUCCESS);
d3038350 458}
fecd2382 459\f
6efd877d 460
fecd2382
RP
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 */
6efd877d
KR
472static void
473perform_an_assembly_pass (argc, argv)
474 int argc;
475 char **argv;
fecd2382 476{
6efd877d 477 int saw_a_file = 0;
67f3dd71
KR
478#ifdef BFD_ASSEMBLER
479 flagword applicable;
480#endif
481
6efd877d
KR
482 need_pass_2 = 0;
483
67f3dd71 484#ifndef BFD_ASSEMBLER
5d9f0ecf 485#ifdef MANY_SEGMENTS
f5200318
KR
486 {
487 unsigned int i;
488 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
6efd877d 489 segment_info[i].fix_root = 0;
f5200318 490 }
6efd877d 491 /* Create the three fixed ones */
6aba9d29
JL
492 {
493 segT seg;
494
ebd6f117
DM
495#ifdef TE_APOLLO
496 seg = subseg_new (".wtext", 0);
497#else
6aba9d29 498 seg = subseg_new (".text", 0);
ebd6f117 499#endif
6aba9d29
JL
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);
ebd6f117
DM
505#ifdef TE_APOLLO
506 create_target_segments ();
507#endif
6aba9d29
JL
508 }
509
e6498b10 510#else /* not MANY_SEGMENTS */
6efd877d
KR
511 text_fix_root = NULL;
512 data_fix_root = NULL;
513 bss_fix_root = NULL;
e6498b10 514#endif /* not MANY_SEGMENTS */
67f3dd71
KR
515#else /* BFD_ASSEMBLER */
516 /* Create the standard sections, and those the assembler uses
517 internally. */
518 text_section = subseg_new (".text", 0);
67f3dd71 519 data_section = subseg_new (".data", 0);
67f3dd71 520 bss_section = subseg_new (".bss", 0);
67f3dd71
KR
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);
8b13fa4e 531 seg_info (bss_section)->bss = 1;
67f3dd71
KR
532 subseg_new (BFD_ABS_SECTION_NAME, 0);
533 subseg_new (BFD_UND_SECTION_NAME, 0);
67f3dd71 534 reg_section = subseg_new ("*GAS `reg' section*", 0);
6aba9d29 535 expr_section = subseg_new ("*GAS `expr' section*", 0);
67f3dd71 536
67f3dd71 537#endif /* BFD_ASSEMBLER */
e6498b10 538
6aba9d29
JL
539 subseg_set (text_section, 0);
540
f5200318
KR
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
6efd877d
KR
545 argv++; /* skip argv[0] */
546 argc--; /* skip argv[0] */
547 while (argc--)
548 {
549 if (*argv)
550 { /* Is it a file-name argument? */
87e48495 551 PROGRESS (1);
6efd877d
KR
552 saw_a_file++;
553 /* argv->"" if stdin desired, else->filename */
554 read_a_source_file (*argv);
a39116f1 555 }
6efd877d
KR
556 argv++; /* completed that argv */
557 }
558 if (!saw_a_file)
559 read_a_source_file ("");
560} /* perform_an_assembly_pass() */
fecd2382 561
a39116f1 562/* end of as.c */
This page took 0.15231 seconds and 4 git commands to generate.