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