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 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();
74 static char * gdb_symbol_file_name;
78 int listing; /* true if a listing is wanted */
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); */
155 listing |= LISTING_LISTING;
159 listing |= LISTING_SYMBOLS;
163 listing |= LISTING_HLL;
168 listing |= LISTING_NOFORM;
172 listing |= LISTING_NODEBUG;
177 listing= LISTING_DEFAULT;
188 break; /* -f means fast - no need for "app" preprocessor. */
191 /* DEBUG is implemented: it debugs different */
192 /* things to other people's assemblers. */
196 case 'G': /* GNU AS switch: include gdbsyms. */
197 if (*arg) /* Rest of argument is file-name. */
198 gdb_symbol_file_name = stralloc (arg);
199 else if (work_argc) { /* Next argument is file-name. */
201 * work_argv = NULL; /* Not a source file-name. */
202 gdb_symbol_file_name = * ++ work_argv;
204 as_warn("%s: I expected a filename after -G", myname);
205 arg = ""; /* Finished with this arg. */
209 case 'I': { /* Include file directory */
213 temp = stralloc (arg);
214 else if (work_argc) {
217 temp = * ++ work_argv;
219 as_warn("%s: I expected a filename after -I", myname);
220 add_include_dir (temp);
221 arg = ""; /* Finished with this arg. */
225 #ifndef WORKING_DOT_WORD
230 case 'L': /* -L means keep L* symbols */
234 if (*arg) /* Rest of argument is object file-name. */
235 out_file_name = stralloc (arg);
236 else if (work_argc) { /* Want next arg for a file-name. */
237 * work_argv = NULL; /* This is not a file-name. */
239 out_file_name = * ++ work_argv;
241 as_warn("%s: I expected a filename after -o. \"%s\" assumed.", myname, out_file_name);
242 arg = ""; /* Finished with this arg. */
246 /* -R means put data into text segment */
252 extern char *compiler_version_string;
253 compiler_version_string = arg;
256 fprintf(stderr,version_string);
257 if(*arg && strcmp(arg,"ersion"))
258 as_warn("Unknown -v option ignored");
260 while(*arg) arg++; /* Skip the rest */
264 /* -W means don't warn about things */
266 /* -X means treat warnings as errors */
268 /* -Z means attempt to generate object file even after errors. */
273 if(md_parse_option(&arg,&work_argc,&work_argv)==0)
274 as_warn("%s: I don't understand '%c' flag.", myname, a);
281 * We have just processed a "-..." arg, which was not a
282 * file-name. Smash it so the
283 * things that look for filenames won't ever see it.
285 * Whatever work_argv points to, it has already been used
286 * as part of a flag, so DON'T re-use it as a filename.
288 *work_argv = NULL; /* NULL means 'not a file-name' */
291 if (gdb_begin(gdb_symbol_file_name) == 0)
292 flagseen ['G'] = 0; /* Don't do any gdbsym stuff. */
294 /* Here with flags set up in flagseen[]. */
295 perform_an_assembly_pass(argc,argv); /* Assemble it. */
299 if (seen_at_least_1_file()
300 && !((had_warnings() && flagseen['Z'])
301 || had_errors() > 0)) {
302 write_object_file(); /* relax() addresses then emit object file */
303 } /* we also check in write_object_file() just before emit. */
306 md_end(); /* MACHINE.c */
313 return((had_warnings() && flagseen['Z'])
314 || had_errors() > 0); /* WIN */
316 return(!((had_warnings() && flagseen['Z'])
317 || had_errors() > 0)); /* WIN */
323 /* perform_an_assembly_pass()
325 * Here to attempt 1 pass over each input file.
326 * We scan argv[*] looking for filenames or exactly "" which is
327 * shorthand for stdin. Any argv that is NULL is not a file-name.
328 * We set need_pass_2 TRUE if, after this, we still have unresolved
329 * expressions of the form (unknown value)+-(unknown value).
331 * Note the un*x semantics: there is only 1 logical input file, but it
332 * may be a catenation of many 'physical' input files.
334 static void perform_an_assembly_pass(argc, argv)
344 for (i= SEG_E0; i < SEG_UNKNOWN; i++)
346 segment_info[i].fix_root = 0;
348 /* Create the three fixed ones */
349 subseg_new (SEG_E0, 0);
350 subseg_new (SEG_E1, 0);
351 subseg_new (SEG_E2, 0);
352 strcpy(segment_info[SEG_E0].scnhdr.s_name,".text");
353 strcpy(segment_info[SEG_E1].scnhdr.s_name,".data");
354 strcpy(segment_info[SEG_E2].scnhdr.s_name,".bss");
356 subseg_new (SEG_E0, 0);
358 text_fix_root = NULL;
359 data_fix_root = NULL;
361 subseg_new (SEG_TEXT, 0);
363 argv++; /* skip argv[0] */
364 argc--; /* skip argv[0] */
366 if (*argv) { /* Is it a file-name argument? */
368 /* argv->"" if stdin desired, else->filename */
369 read_a_source_file(*argv);
371 argv++; /* completed that argv */
374 read_a_source_file("");
375 } /* perform_an_assembly_pass() */
380 * Allocate memory for a new copy of a string. Copy the string.
381 * Return the address of the new string. Die if there is any error.
388 register char * retval;
391 len = strlen (str) + 1;
392 retval = xmalloc (len);
393 (void) strcpy(retval, str);
399 as_fatal("%s: 2nd pass not implemented - get your code from random(3)", myname);
408 static here_before = 0;
410 as_bad("Interrupted by signal %d", sig);