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.
33 * $FreeBSD: src/gnu/usr.bin/as/as.c,v 1.7 1999/08/27 23:34:11 peter Exp $
34 * $DragonFly: src/gnu/usr.bin/as/Attic/as.c,v 1.2 2003/06/17 04:25:44 dillon Exp $
41 #include <sys/types.h> /* For pid_t in signal.h */
51 /* This prototype for got_sig() is ansi. If you want
52 anything else, then your compiler is lying to you when
53 it says that it is __STDC__. If you want to change it,
54 #ifdef protect it from those of us with real ansi
59 static void got_sig(int sig);
60 static char *stralloc(char *str);
61 static void perform_an_assembly_pass(int argc, char **argv);
69 static SIGTY got_sig();
70 static char *stralloc(); /* Make a (safe) copy of a string. */
71 static void perform_an_assembly_pass();
73 #endif /* not __STDC__ */
76 static char * gdb_symbol_file_name;
80 int listing; /* true if a listing is wanted */
82 char *myname; /* argv[0] */
83 extern const char version_string[];
89 int work_argc; /* variable copy of argc */
90 char **work_argv; /* variable copy of argv */
91 char *arg; /* an arg to program */
92 char a; /* an arg flag (after -) */
93 static const int sig[] = { SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
95 for (a=0;sig[a] != 0;a++)
96 if (signal(sig[a], SIG_IGN) != SIG_IGN)
97 signal(sig[a], got_sig);
100 memset(flagseen, '\0', sizeof(flagseen)); /* aint seen nothing yet */
101 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
102 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
103 #endif /* OBJ_DEFAULT_OUTPUT_FILE_NAME */
104 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
106 symbol_begin(); /* symbols.c */
107 subsegs_begin(); /* subsegs.c */
108 read_begin(); /* read.c */
109 md_begin(); /* MACHINE.c */
110 input_scrub_begin(); /* input_scrub.c */
112 gdb_symbol_file_name = 0;
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++) {
128 arg = * work_argv; /* work_argv points to this argument */
130 if (*arg != '-') /* Filename. We need it later. */
131 continue; /* Keep scanning args looking for flags. */
132 if (arg[1] == '-' && arg[2] == 0) {
133 /* "--" as an argument means read STDIN */
134 /* on this scan, we don't want to think about filenames */
135 * work_argv = ""; /* Code that means 'use stdin'. */
138 /* This better be a switch. */
139 arg ++; /*->letter. */
141 while ((a = * arg) != '\0') {/* scan all the 1-char flags */
142 arg ++; /* arg->after letter. */
143 a &= 0x7F; /* ascii only please */
145 as_tsktsk("%s: Flag option - %c has already been seen.", myname, a); */
157 listing |= LISTING_LISTING;
161 listing |= LISTING_SYMBOLS;
165 listing |= LISTING_HLL;
170 listing |= LISTING_NOFORM;
174 listing |= LISTING_NODEBUG;
179 listing= LISTING_DEFAULT;
190 break; /* -f means fast - no need for "app" preprocessor. */
193 /* DEBUG is implemented: it debugs different */
194 /* things to other people's assemblers. */
198 case 'G': /* GNU AS switch: include gdbsyms. */
199 if (*arg) /* Rest of argument is file-name. */
200 gdb_symbol_file_name = stralloc (arg);
201 else if (work_argc) { /* Next argument is file-name. */
203 * work_argv = NULL; /* Not a source file-name. */
204 gdb_symbol_file_name = * ++ work_argv;
206 as_warn("%s: I expected a filename after -G", myname);
207 arg = ""; /* Finished with this arg. */
211 case 'I': { /* Include file directory */
215 temp = stralloc (arg);
216 else if (work_argc) {
219 temp = * ++ work_argv;
221 as_warn("%s: I expected a filename after -I", myname);
222 add_include_dir (temp);
223 arg = ""; /* Finished with this arg. */
227 case 'L': /* -L means keep L* symbols */
231 if (*arg) /* Rest of argument is object file-name. */
232 out_file_name = stralloc (arg);
233 else if (work_argc) { /* Want next arg for a file-name. */
234 * work_argv = NULL; /* This is not a file-name. */
236 out_file_name = * ++ work_argv;
238 as_warn("%s: I expected a filename after -o. \"%s\" assumed.", myname, out_file_name);
239 arg = ""; /* Finished with this arg. */
243 /* -R means put data into text segment */
249 extern char *compiler_version_string;
250 compiler_version_string = arg;
252 #else /* not OBJ_VMS */
253 fprintf(stderr,version_string);
254 if (*arg && strcmp(arg,"ersion"))
255 as_warn("Unknown -v option ignored");
256 #endif /* not OBJ_VMS */
257 while (*arg) arg++; /* Skip the rest */
261 /* -W means don't warn about things */
263 /* -X means treat warnings as errors */
265 /* -Z means attempt to generate object file even after errors. */
270 if (md_parse_option(&arg,&work_argc,&work_argv) == 0)
271 as_warn("%s: I don't understand '%c' flag.", myname, a);
278 * We have just processed a "-..." arg, which was not a
279 * file-name. Smash it so the
280 * things that look for filenames won't ever see it.
282 * Whatever work_argv points to, it has already been used
283 * as part of a flag, so DON'T re-use it as a filename.
285 *work_argv = NULL; /* NULL means 'not a file-name' */
288 if (flagseen['K'] || flagseen['k'])
292 if (gdb_begin(gdb_symbol_file_name) == 0)
293 flagseen['G'] = 0; /* Don't do any gdbsym stuff. */
295 /* Here with flags set up in flagseen[]. */
297 perform_an_assembly_pass(argc,argv); /* Assemble it. */
301 if (seen_at_least_1_file()
302 && !((had_warnings() && flagseen['Z'])
303 || had_errors() > 0)) {
304 write_object_file(); /* relax() addresses then emit object file */
305 } /* we also check in write_object_file() just before emit. */
308 md_end(); /* MACHINE.c */
315 return((had_warnings() && flagseen['Z'])
316 || had_errors() > 0); /* WIN */
318 return(!((had_warnings() && flagseen['Z'])
319 || had_errors() > 0)); /* WIN */
325 /* perform_an_assembly_pass()
327 * Here to attempt 1 pass over each input file.
328 * We scan argv[*] looking for filenames or exactly "" which is
329 * shorthand for stdin. Any argv that is NULL is not a file-name.
330 * We set need_pass_2 TRUE if, after this, we still have unresolved
331 * expressions of the form (unknown value)+-(unknown value).
333 * Note the un*x semantics: there is only 1 logical input file, but it
334 * may be a catenation of many 'physical' input files.
336 static void perform_an_assembly_pass(argc, argv)
346 for (i= SEG_E0; i < SEG_UNKNOWN; i++)
348 segment_info[i].fix_root = 0;
350 /* Create the three fixed ones */
351 subseg_new (SEG_E0, 0);
352 subseg_new (SEG_E1, 0);
353 subseg_new (SEG_E2, 0);
354 strcpy(segment_info[SEG_E0].scnhdr.s_name,".text");
355 strcpy(segment_info[SEG_E1].scnhdr.s_name,".data");
356 strcpy(segment_info[SEG_E2].scnhdr.s_name,".bss");
358 subseg_new (SEG_E0, 0);
359 #else /* not MANY_SEGMENTS */
360 text_fix_root = NULL;
361 data_fix_root = NULL;
364 subseg_new (SEG_TEXT, 0);
365 #endif /* not MANY_SEGMENTS */
367 argv++; /* skip argv[0] */
368 argc--; /* skip argv[0] */
370 if (*argv) { /* Is it a file-name argument? */
372 /* argv->"" if stdin desired, else->filename */
373 read_a_source_file(*argv);
375 argv++; /* completed that argv */
378 read_a_source_file("");
379 } /* perform_an_assembly_pass() */
384 * Allocate memory for a new copy of a string. Copy the string.
385 * Return the address of the new string. Die if there is any error.
392 register char * retval;
395 len = strlen (str) + 1;
396 retval = xmalloc (len);
397 (void) strcpy(retval, str);
403 as_fatal("%s: 2nd pass not implemented - get your code from random(3)", myname);
412 static here_before = 0;
414 as_bad("Interrupted by signal %d", sig);