This commit was manufactured by cvs2svn to create branch 'binutils'.
[external/binutils.git] / gas / as.c
1 /* as.c - GAS main program.
2    Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
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)
9 any later version.
10
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.
15
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. */
19
20 /* static const char rcsid[] = "$Id$"; */
21
22 /*
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
26  * are shared.
27  *
28  *
29  *                      bugs
30  *
31  * : initialisers
32  *      Since no-one else says they will support them in future: I
33  * don't support them now.
34  *
35  */
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #ifdef _POSIX_SOURCE
41 #include <sys/types.h>  /* For pid_t in signal.h */
42 #endif
43 #include <signal.h>
44
45 #define COMMON
46
47 #include "as.h"
48
49 #ifdef __STDC__
50
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
55     compilers. */
56
57 #define SIGTY void
58
59 static void got_sig(int sig);
60 static char *stralloc(char *str);
61 static void perform_an_assembly_pass(int argc, char **argv);
62
63 #else /* __STDC__ */
64
65 #ifndef SIGTY
66 #define SIGTY int
67 #endif
68
69 static SIGTY got_sig();
70 static char *stralloc();        /* Make a (safe) copy of a string. */
71 static void perform_an_assembly_pass();
72
73 #endif /* __STDC__ */
74
75 #ifdef DONTDEF
76 static char * gdb_symbol_file_name;
77 long gdb_begin();
78 #endif
79
80 char *myname;           /* argv[0] */
81 extern char version_string[];
82 \f
83 int main(argc,argv)
84 int argc;
85 char **argv;
86 {
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};
92
93         for(a=0;sig[a]!=0;a++)
94                 if(signal(sig[a], SIG_IGN) != SIG_IGN)
95                         signal(sig[a], got_sig);
96
97         myname=argv[0];
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;
103
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 */
109 #ifdef DONTDEF
110         gdb_symbol_file_name = 0;
111 #endif
112         /*
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.
121          */
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 */
127
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'. */
134                         continue;
135                 }
136                                 /* This better be a switch. */
137                 arg ++;         /*->letter. */
138
139                 while ((a = * arg) != '\0')  {/* scan all the 1-char flags */
140                         arg ++; /* arg->after letter. */
141                         a &= 0x7F;      /* ascii only please */
142                         if (flagseen[a])
143                                 as_tsktsk("%s: Flag option - %c has already been seen.", myname, a);
144                         flagseen[a] = 1;
145                         switch (a) {
146                         case 'f':
147                                 break;  /* -f means fast - no need for "app" preprocessor. */
148
149                         case 'D':
150                                 /* DEBUG is implemented: it debugs different */
151                                 /* things to other people's assemblers. */
152                                 break;
153
154 #ifdef DONTDEF
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. */
159                                         work_argc --;
160                                         * work_argv = NULL; /* Not a source file-name. */
161                                         gdb_symbol_file_name = * ++ work_argv;
162                                 } else
163                                         as_warn("%s: I expected a filename after -G", myname);
164                                 arg = "";       /* Finished with this arg. */
165                                 break;
166 #endif
167
168                         case 'I': { /* Include file directory */
169                                 
170                                 char *temp;
171                                 if (*arg)
172                                     temp = stralloc (arg);
173                                 else if (work_argc) {
174                                         * work_argv = NULL;
175                                         work_argc--;
176                                         temp = * ++ work_argv;
177                                 } else
178                                     as_warn("%s: I expected a filename after -I", myname);
179                                 add_include_dir (temp);
180                                 arg = "";       /* Finished with this arg. */
181                                 break;
182                         }
183
184 #ifndef WORKING_DOT_WORD
185                         case 'k':
186                                 break;
187 #endif
188
189                         case 'L': /* -L means keep L* symbols */
190                                 break;
191
192                         case 'o':
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. */
197                                         work_argc--;
198                                         out_file_name = * ++ work_argv;
199                                 } else
200                                         as_warn("%s: I expected a filename after -o. \"%s\" assumed.", myname, out_file_name);
201                                 arg = "";       /* Finished with this arg. */
202                                 break;
203
204                         case 'R':
205                                 /* -R means put data into text segment */
206                                 break;
207
208                         case 'v':
209 #ifdef  VMS
210                                 {
211                                 extern char *compiler_version_string;
212                                 compiler_version_string = arg;
213                                 }
214 #else /* not VMS */
215                                 fprintf(stderr,version_string);
216                                 if(*arg && strcmp(arg,"ersion"))
217                                         as_warn("Unknown -v option ignored");
218 #endif
219                                 while(*arg) arg++;      /* Skip the rest */
220                                 break;
221
222                         case 'W':
223                                 /* -W means don't warn about things */
224                         case 'X':
225                                 /* -X means treat warnings as errors */
226                         case 'Z':
227                                 /* -Z means attempt to generate object file even after errors. */
228                                 break;
229
230                         default:
231                                 --arg;
232                                 if(md_parse_option(&arg,&work_argc,&work_argv)==0)
233                                         as_warn("%s: I don't understand '%c' flag.", myname, a);
234                                 if(arg && *arg)
235                                         arg++;
236                                 break;
237                         }
238                 }
239                 /*
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.
243                  *
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.
246                  */
247                 *work_argv = NULL; /* NULL means 'not a file-name' */
248         }
249 #ifdef DONTDEF
250         if (gdb_begin(gdb_symbol_file_name) == 0)
251                 flagseen ['G'] = 0;     /* Don't do any gdbsym stuff. */
252 #endif
253         /* Here with flags set up in flagseen[]. */
254         perform_an_assembly_pass(argc,argv); /* Assemble it. */
255 #ifdef TC_I960
256         brtab_emit();
257 #endif
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. */
263
264         input_scrub_end();
265         md_end();                       /* MACHINE.c */
266
267 #ifndef VMS
268         return((had_warnings() && flagseen['Z'])
269                || had_errors() > 0);                    /* WIN */
270 #else   /* VMS */
271         return(!((had_warnings() && flagseen['Z'])
272                  || had_errors() > 0));                 /* WIN */
273 #endif  /* VMS */
274
275 } /* main() */
276
277 \f
278 /*                      perform_an_assembly_pass()
279  *
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).
285  *
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.
288  */
289 static void perform_an_assembly_pass(argc, argv)
290 int argc;
291 char **argv;
292 {
293         int saw_a_file = 0;
294
295         text_fix_root           = NULL;
296         data_fix_root           = NULL;
297         need_pass_2             = 0;
298
299         subseg_new (SEG_TEXT, 0);
300
301         argv++;                 /* skip argv[0] */
302         argc--;                 /* skip argv[0] */
303         while (argc--) {
304                 if (*argv) {            /* Is it a file-name argument? */
305                         saw_a_file++;
306                         /* argv->"" if stdin desired, else->filename */
307                         read_a_source_file(*argv);
308                 }
309                 argv++;                 /* completed that argv */
310         }
311         if(!saw_a_file)
312                 read_a_source_file("");
313 } /* perform_an_assembly_pass() */
314 \f
315 /*
316  *                      stralloc()
317  *
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.
320  */
321
322 static char *
323 stralloc (str)
324 char *  str;
325 {
326         register char * retval;
327         register long len;
328
329         len = strlen (str) + 1;
330         retval = xmalloc (len);
331         (void) strcpy(retval, str);
332         return(retval);
333 }
334 \f
335 #ifdef comment
336 static void lose() {
337         as_fatal("%s: 2nd pass not implemented - get your code from random(3)", myname);
338         return;
339 } /* lose() */
340 #endif /* comment */
341
342 static SIGTY
343 got_sig(sig)
344 int sig;
345 {
346         static here_before = 0;
347
348         as_bad("Interrupted by signal %d", sig);
349         if(here_before++)
350                 exit(1);
351         return((SIGTY) 0);
352 }
353
354 /*
355  * Local Variables:
356  * comment-column: 0
357  * fill-column: 131
358  * End:
359  */
360
361 /* end: as.c */