1 /* as.c - GAS main program.
2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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. */
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
30 * Since no-one else says they will support them in future: I
31 * don't support them now.
39 #include <sys/types.h> /* For pid_t in signal.h */
49 /* This prototype for got_sig() is ansi. If you want
50 anything else, then your compiler is lying to you when
51 it says that it is __STDC__. If you want to change it,
52 #ifdef protect it from those of us with real ansi
57 static void got_sig (int sig
);
58 static char *stralloc (char *str
);
59 static void perform_an_assembly_pass (int argc
, char **argv
);
67 static SIGTY
got_sig ();
68 static char *stralloc (); /* Make a (safe) copy of a string. */
69 static void perform_an_assembly_pass ();
71 #endif /* not __STDC__ */
73 int listing
; /* true if a listing is wanted */
75 char *myname
; /* argv[0] */
76 extern char version_string
[];
78 segT big_section
, reg_section
, pass1_section
;
79 segT diff_section
, absent_section
;
80 segT text_section
, data_section
, bss_section
;
88 int work_argc
; /* variable copy of argc */
89 char **work_argv
; /* variable copy of argv */
90 char *arg
; /* an arg to program */
91 char a
; /* an arg flag (after -) */
92 static const int sig
[] = {SIGHUP
, SIGINT
, SIGPIPE
, SIGTERM
, 0};
94 for (a
= 0; sig
[a
] != 0; a
++)
95 if (signal (sig
[a
], SIG_IGN
) != SIG_IGN
)
96 signal (sig
[a
], got_sig
);
99 memset (flagseen
, '\0', sizeof (flagseen
)); /* aint seen nothing yet */
100 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
101 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
102 #endif /* OBJ_DEFAULT_OUTPUT_FILE_NAME */
103 out_file_name
= OBJ_DEFAULT_OUTPUT_FILE_NAME
;
109 symbol_begin (); /* symbols.c */
110 subsegs_begin (); /* subsegs.c */
111 read_begin (); /* read.c */
112 md_begin (); /* MACHINE.c */
113 input_scrub_begin (); /* input_scrub.c */
115 * Parse arguments, but we are only interested in flags.
116 * When we find a flag, we process it then make it's argv[] NULL.
117 * This helps any future argv[] scanners avoid what we processed.
118 * Since it is easy to do here we interpret the special arg "-"
119 * to mean "use stdin" and we set that argv[] pointing to "".
120 * After we have munged argv[], the only things left are source file
121 * name(s) and ""(s) denoting stdin. These file names are used
122 * (perhaps more than once) later.
124 /* FIXME-SOMEDAY this should use getopt. */
125 work_argc
= argc
- 1; /* don't count argv[0] */
126 work_argv
= argv
+ 1; /* skip argv[0] */
127 for (; work_argc
--; work_argv
++)
129 arg
= *work_argv
; /* work_argv points to this argument */
131 if (*arg
!= '-') /* Filename. We need it later. */
132 continue; /* Keep scanning args looking for flags. */
133 if (arg
[1] == '-' && arg
[2] == 0)
135 /* "--" as an argument means read STDIN */
136 /* on this scan, we don't want to think about filenames */
137 *work_argv
= ""; /* Code that means 'use stdin'. */
140 /* This better be a switch. */
141 arg
++; /*->letter. */
143 while ((a
= *arg
) != '\0')
144 { /* scan all the 1-char flags */
145 arg
++; /* arg->after letter. */
146 a
&= 0x7F; /* ascii only please */
159 listing
|= LISTING_LISTING
;
163 listing
|= LISTING_SYMBOLS
;
167 listing
|= LISTING_HLL
;
172 listing
|= LISTING_NOFORM
;
176 listing
|= LISTING_NODEBUG
;
181 listing
= LISTING_DEFAULT
;
192 break; /* -f means fast - no need for "app" preprocessor. */
195 /* DEBUG is implemented: it debugs different */
196 /* things to other people's assemblers. */
200 { /* Include file directory */
204 temp
= stralloc (arg
);
212 as_warn ("%s: I expected a filename after -I", myname
);
213 add_include_dir (temp
);
214 arg
= ""; /* Finished with this arg. */
218 #ifdef WARN_SIGNED_OVERFLOW_WORD
219 /* Don't warn about signed overflow. */
224 #ifndef WORKING_DOT_WORD
229 case 'L': /* -L means keep L* symbols */
233 if (*arg
) /* Rest of argument is object file-name. */
234 out_file_name
= stralloc (arg
);
236 { /* Want next arg for a file-name. */
237 *work_argv
= NULL
; /* This is not a file-name. */
239 out_file_name
= *++work_argv
;
242 as_warn ("%s: I expected a filename after -o. \"%s\" assumed.", myname
, out_file_name
);
243 arg
= ""; /* Finished with this arg. */
247 /* -R means put data into text segment */
248 flag_readonly_data_in_text
= 1;
254 extern char *compiler_version_string
;
255 compiler_version_string
= arg
;
258 fprintf (stderr
, version_string
);
259 if (*arg
&& strcmp (arg
, "ersion"))
260 as_warn ("Unknown -v option ignored");
263 arg
++; /* Skip the rest */
267 /* -W means don't warn about things */
268 flag_suppress_warnings
= 1;
273 /* -X means treat warnings as errors */
276 /* -Z means attempt to generate object file even after errors. */
277 flag_always_generate_output
= 1;
282 if (md_parse_option (&arg
, &work_argc
, &work_argv
) == 0)
283 as_warn ("%s: I don't understand '%c' flag.", myname
, a
);
290 * We have just processed a "-..." arg, which was not a
291 * file-name. Smash it so the
292 * things that look for filenames won't ever see it.
294 * Whatever work_argv points to, it has already been used
295 * as part of a flag, so DON'T re-use it as a filename.
297 *work_argv
= NULL
; /* NULL means 'not a file-name' */
301 output_file_create (out_file_name
);
302 assert (stdoutput
!= 0);
305 /* Here with flags set up in flagseen[]. */
306 perform_an_assembly_pass (argc
, argv
); /* Assemble it. */
310 if (seen_at_least_1_file ()
311 && !((had_warnings () && flag_always_generate_output
)
312 || had_errors () > 0))
314 write_object_file (); /* relax() addresses then emit object file */
315 } /* we also check in write_object_file() just before emit. */
318 md_end (); /* MACHINE.c */
325 return ((had_warnings () && flagseen
['Z'])
326 || had_errors () > 0);/* WIN */
328 return (!((had_warnings () && flagseen
['Z'])
329 || had_errors () > 0)); /* WIN */
335 /* perform_an_assembly_pass()
337 * Here to attempt 1 pass over each input file.
338 * We scan argv[*] looking for filenames or exactly "" which is
339 * shorthand for stdin. Any argv that is NULL is not a file-name.
340 * We set need_pass_2 TRUE if, after this, we still have unresolved
341 * expressions of the form (unknown value)+-(unknown value).
343 * Note the un*x semantics: there is only 1 logical input file, but it
344 * may be a catenation of many 'physical' input files.
347 perform_an_assembly_pass (argc
, argv
)
359 #ifndef BFD_ASSEMBLER
361 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
363 segment_info
[i
].fix_root
= 0;
365 /* Create the three fixed ones */
366 subseg_new (SEG_E0
, 0);
367 subseg_new (SEG_E1
, 0);
368 subseg_new (SEG_E2
, 0);
369 strcpy (segment_info
[SEG_E0
].scnhdr
.s_name
, ".text");
370 strcpy (segment_info
[SEG_E1
].scnhdr
.s_name
, ".data");
371 strcpy (segment_info
[SEG_E2
].scnhdr
.s_name
, ".bss");
373 subseg_new (SEG_E0
, 0);
374 #else /* not MANY_SEGMENTS */
375 text_fix_root
= NULL
;
376 data_fix_root
= NULL
;
379 subseg_new (SEG_TEXT
, 0);
380 #endif /* not MANY_SEGMENTS */
381 #else /* BFD_ASSEMBLER */
382 /* Create the standard sections, and those the assembler uses
384 text_section
= subseg_new (".text", 0);
385 text_section
->output_section
= text_section
;
386 data_section
= subseg_new (".data", 0);
387 data_section
->output_section
= data_section
;
388 bss_section
= subseg_new (".bss", 0);
389 bss_section
->output_section
= bss_section
;
390 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
391 to have relocs, otherwise we don't find out in time. */
392 applicable
= bfd_applicable_section_flags (stdoutput
);
393 bfd_set_section_flags (stdoutput
, text_section
,
394 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
395 | SEC_CODE
| SEC_READONLY
));
396 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
397 bfd_set_section_flags (stdoutput
, data_section
,
398 applicable
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
));
399 bfd_set_section_flags (stdoutput
, bss_section
, applicable
& SEC_ALLOC
);
400 subseg_new (BFD_ABS_SECTION_NAME
, 0);
401 subseg_new (BFD_UND_SECTION_NAME
, 0);
402 big_section
= subseg_new ("*GAS `big' section*", 0);
403 reg_section
= subseg_new ("*GAS `reg' section*", 0);
404 pass1_section
= subseg_new ("*GAS `pass1' section*", 0);
405 diff_section
= subseg_new ("*GAS `diff' section*", 0);
406 absent_section
= subseg_new ("*GAS `absent' section*", 0);
408 subseg_new (".text", 0);
409 #endif /* BFD_ASSEMBLER */
411 argv
++; /* skip argv[0] */
412 argc
--; /* skip argv[0] */
416 { /* Is it a file-name argument? */
418 /* argv->"" if stdin desired, else->filename */
419 read_a_source_file (*argv
);
421 argv
++; /* completed that argv */
424 read_a_source_file ("");
425 } /* perform_an_assembly_pass() */
430 * Allocate memory for a new copy of a string. Copy the string.
431 * Return the address of the new string. Die if there is any error.
438 register char *retval
;
441 len
= strlen (str
) + 1;
442 retval
= xmalloc (len
);
443 (void) strcpy (retval
, str
);
451 static here_before
= 0;
453 as_bad ("Interrupted by signal %d", sig
);