1 /* as.c - GAS main program.
2 Copyright (C) 1987, 1990, 1991 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 1, 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. */
20 /* static const char rcsid[] = "$Id$"; */
23 * Main program for AS; a 32-bit assembler of GNU.
24 * Understands command arguments.
25 * Has a few routines that don't fit in other modules because they
32 * Since no-one else says they will support them in future: I
33 * don't support them now.
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();
76 static char * gdb_symbol_file_name;
80 char *myname; /* argv[0] */
81 extern char version_string[];
87 int work_argc; /* variable copy of argc */
88 char **work_argv; /* variable copy of argv */
89 char *arg; /* an arg to program */
90 char a; /* an arg flag (after -) */
91 static const int sig[] = { SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
93 for(a=0;sig[a]!=0;a++)
94 if(signal(sig[a], SIG_IGN) != SIG_IGN)
95 signal(sig[a], got_sig);
98 bzero (flagseen, sizeof(flagseen)); /* aint seen nothing yet */
99 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
100 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
101 #endif /* OBJ_DEFAULT_OUTPUT_FILE_NAME */
102 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
104 symbol_begin(); /* symbols.c */
105 subsegs_begin(); /* subsegs.c */
106 read_begin(); /* read.c */
107 md_begin(); /* MACHINE.c */
108 input_scrub_begin(); /* input_scrub.c */
110 gdb_symbol_file_name = 0;
113 * Parse arguments, but we are only interested in flags.
114 * When we find a flag, we process it then make it's argv[] NULL.
115 * This helps any future argv[] scanners avoid what we processed.
116 * Since it is easy to do here we interpret the special arg "-"
117 * to mean "use stdin" and we set that argv[] pointing to "".
118 * After we have munged argv[], the only things left are source file
119 * name(s) and ""(s) denoting stdin. These file names are used
120 * (perhaps more than once) later.
122 /* FIXME-SOMEDAY this should use getopt. */
123 work_argc = argc-1; /* don't count argv[0] */
124 work_argv = argv+1; /* skip argv[0] */
125 for (;work_argc--;work_argv++) {
126 arg = * work_argv; /* work_argv points to this argument */
128 if (*arg!='-') /* Filename. We need it later. */
129 continue; /* Keep scanning args looking for flags. */
130 if (arg[1] == '-' && arg[2] == 0) {
131 /* "--" as an argument means read STDIN */
132 /* on this scan, we don't want to think about filenames */
133 * work_argv = ""; /* Code that means 'use stdin'. */
136 /* This better be a switch. */
137 arg ++; /*->letter. */
139 while ((a = * arg) != '\0') {/* scan all the 1-char flags */
140 arg ++; /* arg->after letter. */
141 a &= 0x7F; /* ascii only please */
143 as_tsktsk("%s: Flag option - %c has already been seen.", myname, a);
147 break; /* -f means fast - no need for "app" preprocessor. */
150 /* DEBUG is implemented: it debugs different */
151 /* things to other people's assemblers. */
155 case 'G': /* GNU AS switch: include gdbsyms. */
156 if (*arg) /* Rest of argument is file-name. */
157 gdb_symbol_file_name = stralloc (arg);
158 else if (work_argc) { /* Next argument is file-name. */
160 * work_argv = NULL; /* Not a source file-name. */
161 gdb_symbol_file_name = * ++ work_argv;
163 as_warn("%s: I expected a filename after -G", myname);
164 arg = ""; /* Finished with this arg. */
168 case 'I': { /* Include file directory */
172 temp = stralloc (arg);
173 else if (work_argc) {
176 temp = * ++ work_argv;
178 as_warn("%s: I expected a filename after -I", myname);
179 add_include_dir (temp);
180 arg = ""; /* Finished with this arg. */
184 #ifndef WORKING_DOT_WORD
189 case 'L': /* -L means keep L* symbols */
193 if (*arg) /* Rest of argument is object file-name. */
194 out_file_name = stralloc (arg);
195 else if (work_argc) { /* Want next arg for a file-name. */
196 * work_argv = NULL; /* This is not a file-name. */
198 out_file_name = * ++ work_argv;
200 as_warn("%s: I expected a filename after -o. \"%s\" assumed.", myname, out_file_name);
201 arg = ""; /* Finished with this arg. */
205 /* -R means put data into text segment */
211 extern char *compiler_version_string;
212 compiler_version_string = arg;
215 fprintf(stderr,version_string);
216 if(*arg && strcmp(arg,"ersion"))
217 as_warn("Unknown -v option ignored");
219 while(*arg) arg++; /* Skip the rest */
223 /* -W means don't warn about things */
225 /* -X means treat warnings as errors */
227 /* -Z means attempt to generate object file even after errors. */
232 if(md_parse_option(&arg,&work_argc,&work_argv)==0)
233 as_warn("%s: I don't understand '%c' flag.", myname, a);
240 * We have just processed a "-..." arg, which was not a
241 * file-name. Smash it so the
242 * things that look for filenames won't ever see it.
244 * Whatever work_argv points to, it has already been used
245 * as part of a flag, so DON'T re-use it as a filename.
247 *work_argv = NULL; /* NULL means 'not a file-name' */
250 if (gdb_begin(gdb_symbol_file_name) == 0)
251 flagseen ['G'] = 0; /* Don't do any gdbsym stuff. */
253 /* Here with flags set up in flagseen[]. */
254 perform_an_assembly_pass(argc,argv); /* Assemble it. */
258 if (seen_at_least_1_file()
259 && !((had_warnings() && flagseen['Z'])
260 || had_errors() > 0)) {
261 write_object_file(); /* relax() addresses then emit object file */
262 } /* we also check in write_object_file() just before emit. */
265 md_end(); /* MACHINE.c */
268 return((had_warnings() && flagseen['Z'])
269 || had_errors() > 0); /* WIN */
271 return(!((had_warnings() && flagseen['Z'])
272 || had_errors() > 0)); /* WIN */
278 /* perform_an_assembly_pass()
280 * Here to attempt 1 pass over each input file.
281 * We scan argv[*] looking for filenames or exactly "" which is
282 * shorthand for stdin. Any argv that is NULL is not a file-name.
283 * We set need_pass_2 TRUE if, after this, we still have unresolved
284 * expressions of the form (unknown value)+-(unknown value).
286 * Note the un*x semantics: there is only 1 logical input file, but it
287 * may be a catenation of many 'physical' input files.
289 static void perform_an_assembly_pass(argc, argv)
295 text_fix_root = NULL;
296 data_fix_root = NULL;
299 subseg_new (SEG_TEXT, 0);
301 argv++; /* skip argv[0] */
302 argc--; /* skip argv[0] */
304 if (*argv) { /* Is it a file-name argument? */
306 /* argv->"" if stdin desired, else->filename */
307 read_a_source_file(*argv);
309 argv++; /* completed that argv */
312 read_a_source_file("");
313 } /* perform_an_assembly_pass() */
318 * Allocate memory for a new copy of a string. Copy the string.
319 * Return the address of the new string. Die if there is any error.
326 register char * retval;
329 len = strlen (str) + 1;
330 retval = xmalloc (len);
331 (void) strcpy(retval, str);
337 as_fatal("%s: 2nd pass not implemented - get your code from random(3)", myname);
346 static here_before = 0;
348 as_bad("Interrupted by signal %d", sig);