* as.c: change -l to -a[lsn], to avoid conflict with 68000
[deliverable/binutils-gdb.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 2, 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 #include "subsegs.h"
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 int listing; /* true if a listing is wanted */
81
82 char *myname; /* argv[0] */
83 extern char version_string[];
84 \f
85 int main(argc,argv)
86 int argc;
87 char **argv;
88 {
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};
94
95 for(a=0;sig[a]!=0;a++)
96 if(signal(sig[a], SIG_IGN) != SIG_IGN)
97 signal(sig[a], got_sig);
98
99 myname=argv[0];
100 bzero (flagseen, 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;
105
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 */
111 #ifdef DONTDEF
112 gdb_symbol_file_name = 0;
113 #endif
114 /*
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.
123 */
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 */
129
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'. */
136 continue;
137 }
138 /* This better be a switch. */
139 arg ++; /*->letter. */
140
141 while ((a = * arg) != '\0') {/* scan all the 1-char flags */
142 arg ++; /* arg->after letter. */
143 a &= 0x7F; /* ascii only please */
144 /* if (flagseen[a])
145 as_tsktsk("%s: Flag option - %c has already been seen.", myname, a); */
146 flagseen[a] = 1;
147 switch (a) {
148
149 case 'a':
150 {
151 int loop =1;
152
153 while (loop) {
154 switch (*arg)
155 {
156 case 'l':
157 listing |= LISTING_LISTING;
158 arg++;
159 break;
160 case 's':
161 listing |= LISTING_SYMBOLS;
162 arg++;
163 break;
164 case 'n':
165 listing |= LISTING_NOFORM;
166 arg++;
167 break;
168 default:
169 if (!listing)
170 listing= LISTING_DEFAULT;
171 loop = 0;
172 break;
173 }
174 }
175 }
176
177 break;
178
179
180 case 'f':
181 break; /* -f means fast - no need for "app" preprocessor. */
182
183 case 'D':
184 /* DEBUG is implemented: it debugs different */
185 /* things to other people's assemblers. */
186 break;
187
188 #ifdef DONTDEF
189 case 'G': /* GNU AS switch: include gdbsyms. */
190 if (*arg) /* Rest of argument is file-name. */
191 gdb_symbol_file_name = stralloc (arg);
192 else if (work_argc) { /* Next argument is file-name. */
193 work_argc --;
194 * work_argv = NULL; /* Not a source file-name. */
195 gdb_symbol_file_name = * ++ work_argv;
196 } else
197 as_warn("%s: I expected a filename after -G", myname);
198 arg = ""; /* Finished with this arg. */
199 break;
200 #endif
201
202 case 'I': { /* Include file directory */
203
204 char *temp = NULL;
205 if (*arg)
206 temp = stralloc (arg);
207 else if (work_argc) {
208 * work_argv = NULL;
209 work_argc--;
210 temp = * ++ work_argv;
211 } else
212 as_warn("%s: I expected a filename after -I", myname);
213 add_include_dir (temp);
214 arg = ""; /* Finished with this arg. */
215 break;
216 }
217
218 #ifndef WORKING_DOT_WORD
219 case 'k':
220 break;
221 #endif
222
223 case 'L': /* -L means keep L* symbols */
224 break;
225
226 case 'o':
227 if (*arg) /* Rest of argument is object file-name. */
228 out_file_name = stralloc (arg);
229 else if (work_argc) { /* Want next arg for a file-name. */
230 * work_argv = NULL; /* This is not a file-name. */
231 work_argc--;
232 out_file_name = * ++ work_argv;
233 } else
234 as_warn("%s: I expected a filename after -o. \"%s\" assumed.", myname, out_file_name);
235 arg = ""; /* Finished with this arg. */
236 break;
237
238 case 'R':
239 /* -R means put data into text segment */
240 break;
241
242 case 'v':
243 #ifdef VMS
244 {
245 extern char *compiler_version_string;
246 compiler_version_string = arg;
247 }
248 #else /* not VMS */
249 fprintf(stderr,version_string);
250 if(*arg && strcmp(arg,"ersion"))
251 as_warn("Unknown -v option ignored");
252 #endif
253 while(*arg) arg++; /* Skip the rest */
254 break;
255
256 case 'W':
257 /* -W means don't warn about things */
258 case 'X':
259 /* -X means treat warnings as errors */
260 case 'Z':
261 /* -Z means attempt to generate object file even after errors. */
262 break;
263
264 default:
265 --arg;
266 if(md_parse_option(&arg,&work_argc,&work_argv)==0)
267 as_warn("%s: I don't understand '%c' flag.", myname, a);
268 if(arg && *arg)
269 arg++;
270 break;
271 }
272 }
273 /*
274 * We have just processed a "-..." arg, which was not a
275 * file-name. Smash it so the
276 * things that look for filenames won't ever see it.
277 *
278 * Whatever work_argv points to, it has already been used
279 * as part of a flag, so DON'T re-use it as a filename.
280 */
281 *work_argv = NULL; /* NULL means 'not a file-name' */
282 }
283 #ifdef DONTDEF
284 if (gdb_begin(gdb_symbol_file_name) == 0)
285 flagseen ['G'] = 0; /* Don't do any gdbsym stuff. */
286 #endif
287 /* Here with flags set up in flagseen[]. */
288 perform_an_assembly_pass(argc,argv); /* Assemble it. */
289 #ifdef TC_I960
290 brtab_emit();
291 #endif
292 if (seen_at_least_1_file()
293 && !((had_warnings() && flagseen['Z'])
294 || had_errors() > 0)) {
295 write_object_file(); /* relax() addresses then emit object file */
296 } /* we also check in write_object_file() just before emit. */
297
298 input_scrub_end();
299 md_end(); /* MACHINE.c */
300
301 #ifndef NO_LISTING
302 listing_print();
303 #endif
304
305 #ifndef VMS
306 return((had_warnings() && flagseen['Z'])
307 || had_errors() > 0); /* WIN */
308 #else /* VMS */
309 return(!((had_warnings() && flagseen['Z'])
310 || had_errors() > 0)); /* WIN */
311 #endif /* VMS */
312
313 } /* main() */
314
315 \f
316 /* perform_an_assembly_pass()
317 *
318 * Here to attempt 1 pass over each input file.
319 * We scan argv[*] looking for filenames or exactly "" which is
320 * shorthand for stdin. Any argv that is NULL is not a file-name.
321 * We set need_pass_2 TRUE if, after this, we still have unresolved
322 * expressions of the form (unknown value)+-(unknown value).
323 *
324 * Note the un*x semantics: there is only 1 logical input file, but it
325 * may be a catenation of many 'physical' input files.
326 */
327 static void perform_an_assembly_pass(argc, argv)
328 int argc;
329 char **argv;
330 {
331 int saw_a_file = 0;
332 unsigned int i;
333 need_pass_2 = 0;
334
335 #ifdef MANY_SEGMENTS
336
337 for (i= SEG_E0; i < SEG_UNKNOWN; i++)
338 {
339 segment_info[i].fix_root = 0;
340 }
341 /* Create the three fixed ones */
342 subseg_new (SEG_E0, 0);
343 subseg_new (SEG_E1, 0);
344 subseg_new (SEG_E2, 0);
345 strcpy(segment_info[SEG_E0].scnhdr.s_name,".text");
346 strcpy(segment_info[SEG_E1].scnhdr.s_name,".data");
347 strcpy(segment_info[SEG_E2].scnhdr.s_name,".bss");
348
349 subseg_new (SEG_E0, 0);
350 #else
351 text_fix_root = NULL;
352 data_fix_root = NULL;
353
354 subseg_new (SEG_TEXT, 0);
355 #endif
356 argv++; /* skip argv[0] */
357 argc--; /* skip argv[0] */
358 while (argc--) {
359 if (*argv) { /* Is it a file-name argument? */
360 saw_a_file++;
361 /* argv->"" if stdin desired, else->filename */
362 read_a_source_file(*argv);
363 }
364 argv++; /* completed that argv */
365 }
366 if(!saw_a_file)
367 read_a_source_file("");
368 } /* perform_an_assembly_pass() */
369 \f
370 /*
371 * stralloc()
372 *
373 * Allocate memory for a new copy of a string. Copy the string.
374 * Return the address of the new string. Die if there is any error.
375 */
376
377 static char *
378 stralloc (str)
379 char * str;
380 {
381 register char * retval;
382 register long len;
383
384 len = strlen (str) + 1;
385 retval = xmalloc (len);
386 (void) strcpy(retval, str);
387 return(retval);
388 }
389 \f
390 #ifdef comment
391 static void lose() {
392 as_fatal("%s: 2nd pass not implemented - get your code from random(3)", myname);
393 return;
394 } /* lose() */
395 #endif /* comment */
396
397 static SIGTY
398 got_sig(sig)
399 int sig;
400 {
401 static here_before = 0;
402
403 as_bad("Interrupted by signal %d", sig);
404 if(here_before++)
405 exit(1);
406 return((SIGTY) 0);
407 }
408
409 /*
410 * Local Variables:
411 * comment-column: 0
412 * fill-column: 131
413 * End:
414 */
415
416 /* end: as.c */
This page took 0.130782 seconds and 5 git commands to generate.