more bfd merging - see ChangeLog
[binutils-gdb.git] / gas / as.c
1 /* as.c - GAS main program.
2 Copyright (C) 1987, 1990, 1991, 1992 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 /*
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
24 * are shared.
25 *
26 *
27 * bugs
28 *
29 * : initialisers
30 * Since no-one else says they will support them in future: I
31 * don't support them now.
32 *
33 */
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #ifdef _POSIX_SOURCE
39 #include <sys/types.h> /* For pid_t in signal.h */
40 #endif
41 #include <signal.h>
42
43 #define COMMON
44
45 #include "as.h"
46 #include "subsegs.h"
47 #if __STDC__ == 1
48
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
53 compilers. */
54
55 #define SIGTY void
56
57 static void got_sig (int sig);
58 static char *stralloc (char *str);
59 static void perform_an_assembly_pass (int argc, char **argv);
60
61 #else /* __STDC__ */
62
63 #ifndef SIGTY
64 #define SIGTY int
65 #endif
66
67 static SIGTY got_sig ();
68 static char *stralloc (); /* Make a (safe) copy of a string. */
69 static void perform_an_assembly_pass ();
70
71 #endif /* not __STDC__ */
72
73 int listing; /* true if a listing is wanted */
74
75 char *myname; /* argv[0] */
76 extern char version_string[];
77 #ifdef BFD_ASSEMBLER
78 segT big_section, reg_section, pass1_section;
79 segT diff_section, absent_section;
80 segT text_section, data_section, bss_section;
81 #endif
82 \f
83 int
84 main (argc, argv)
85 int argc;
86 char **argv;
87 {
88 int work_argc; /* variable copy of argc */
89 char **work_argv; /* variable copy of argv */
90 char *arg; /* an arg to program */
91 char a; /* an arg flag (after -) */
92 static const int sig[] = {SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
93
94 for (a = 0; sig[a] != 0; a++)
95 if (signal (sig[a], SIG_IGN) != SIG_IGN)
96 signal (sig[a], got_sig);
97
98 myname = argv[0];
99 memset (flagseen, '\0', sizeof (flagseen)); /* aint seen nothing yet */
100 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
101 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
102 #endif /* OBJ_DEFAULT_OUTPUT_FILE_NAME */
103 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
104
105 #ifdef BFD_ASSEMBLER
106 bfd_init ();
107 #endif
108
109 symbol_begin (); /* symbols.c */
110 subsegs_begin (); /* subsegs.c */
111 read_begin (); /* read.c */
112 md_begin (); /* MACHINE.c */
113 input_scrub_begin (); /* input_scrub.c */
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 {
129 arg = *work_argv; /* work_argv points to this argument */
130
131 if (*arg != '-') /* Filename. We need it later. */
132 continue; /* Keep scanning args looking for flags. */
133 if (arg[1] == '-' && arg[2] == 0)
134 {
135 /* "--" as an argument means read STDIN */
136 /* on this scan, we don't want to think about filenames */
137 *work_argv = ""; /* Code that means 'use stdin'. */
138 continue;
139 }
140 /* This better be a switch. */
141 arg++; /*->letter. */
142
143 while ((a = *arg) != '\0')
144 { /* scan all the 1-char flags */
145 arg++; /* arg->after letter. */
146 a &= 0x7F; /* ascii only please */
147 flagseen[a] = 1;
148 switch (a)
149 {
150 case 'a':
151 {
152 int loop = 1;
153
154 while (loop)
155 {
156 switch (*arg)
157 {
158 case 'l':
159 listing |= LISTING_LISTING;
160 arg++;
161 break;
162 case 's':
163 listing |= LISTING_SYMBOLS;
164 arg++;
165 break;
166 case 'h':
167 listing |= LISTING_HLL;
168 arg++;
169 break;
170
171 case 'n':
172 listing |= LISTING_NOFORM;
173 arg++;
174 break;
175 case 'd':
176 listing |= LISTING_NODEBUG;
177 arg++;
178 break;
179 default:
180 if (!listing)
181 listing = LISTING_DEFAULT;
182 loop = 0;
183 break;
184 }
185 }
186 }
187
188 break;
189
190
191 case 'f':
192 break; /* -f means fast - no need for "app" preprocessor. */
193
194 case 'D':
195 /* DEBUG is implemented: it debugs different */
196 /* things to other people's assemblers. */
197 break;
198
199 case 'I':
200 { /* Include file directory */
201
202 char *temp = NULL;
203 if (*arg)
204 temp = stralloc (arg);
205 else if (work_argc)
206 {
207 *work_argv = NULL;
208 work_argc--;
209 temp = *++work_argv;
210 }
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 #ifdef WARN_SIGNED_OVERFLOW_WORD
219 /* Don't warn about signed overflow. */
220 case 'J':
221 break;
222 #endif
223
224 #ifndef WORKING_DOT_WORD
225 case 'K':
226 break;
227 #endif
228
229 case 'L': /* -L means keep L* symbols */
230 break;
231
232 case 'o':
233 if (*arg) /* Rest of argument is object file-name. */
234 out_file_name = stralloc (arg);
235 else if (work_argc)
236 { /* Want next arg for a file-name. */
237 *work_argv = NULL; /* This is not a file-name. */
238 work_argc--;
239 out_file_name = *++work_argv;
240 }
241 else
242 as_warn ("%s: I expected a filename after -o. \"%s\" assumed.", myname, out_file_name);
243 arg = ""; /* Finished with this arg. */
244 break;
245
246 case 'R':
247 /* -R means put data into text segment */
248 flag_readonly_data_in_text = 1;
249 break;
250
251 case 'v':
252 #ifdef VMS
253 {
254 extern char *compiler_version_string;
255 compiler_version_string = arg;
256 }
257 #else /* not VMS */
258 fprintf (stderr, version_string);
259 if (*arg && strcmp (arg, "ersion"))
260 as_warn ("Unknown -v option ignored");
261 #endif
262 while (*arg)
263 arg++; /* Skip the rest */
264 break;
265
266 case 'W':
267 /* -W means don't warn about things */
268 flag_suppress_warnings = 1;
269 break;
270
271 case 'w':
272 case 'X':
273 /* -X means treat warnings as errors */
274 break;
275 case 'Z':
276 /* -Z means attempt to generate object file even after errors. */
277 flag_always_generate_output = 1;
278 break;
279
280 default:
281 --arg;
282 if (md_parse_option (&arg, &work_argc, &work_argv) == 0)
283 as_warn ("%s: I don't understand '%c' flag.", myname, a);
284 if (arg && *arg)
285 arg++;
286 break;
287 }
288 }
289 /*
290 * We have just processed a "-..." arg, which was not a
291 * file-name. Smash it so the
292 * things that look for filenames won't ever see it.
293 *
294 * Whatever work_argv points to, it has already been used
295 * as part of a flag, so DON'T re-use it as a filename.
296 */
297 *work_argv = NULL; /* NULL means 'not a file-name' */
298 }
299
300 #ifdef BFD_ASSEMBLER
301 output_file_create (out_file_name);
302 assert (stdoutput != 0);
303 #endif
304
305 /* Here with flags set up in flagseen[]. */
306 perform_an_assembly_pass (argc, argv); /* Assemble it. */
307 #ifdef TC_I960
308 brtab_emit ();
309 #endif
310 if (seen_at_least_1_file ()
311 && !((had_warnings () && flag_always_generate_output)
312 || had_errors () > 0))
313 {
314 write_object_file (); /* relax() addresses then emit object file */
315 } /* we also check in write_object_file() just before emit. */
316
317 input_scrub_end ();
318 md_end (); /* MACHINE.c */
319
320 #ifndef NO_LISTING
321 listing_print ("");
322 #endif
323
324 #ifndef VMS
325 return ((had_warnings () && flagseen['Z'])
326 || had_errors () > 0);/* WIN */
327 #else /* VMS */
328 return (!((had_warnings () && flagseen['Z'])
329 || had_errors () > 0)); /* WIN */
330 #endif /* VMS */
331
332 } /* main() */
333 \f
334
335 /* perform_an_assembly_pass()
336 *
337 * Here to attempt 1 pass over each input file.
338 * We scan argv[*] looking for filenames or exactly "" which is
339 * shorthand for stdin. Any argv that is NULL is not a file-name.
340 * We set need_pass_2 TRUE if, after this, we still have unresolved
341 * expressions of the form (unknown value)+-(unknown value).
342 *
343 * Note the un*x semantics: there is only 1 logical input file, but it
344 * may be a catenation of many 'physical' input files.
345 */
346 static void
347 perform_an_assembly_pass (argc, argv)
348 int argc;
349 char **argv;
350 {
351 int saw_a_file = 0;
352 unsigned int i;
353 #ifdef BFD_ASSEMBLER
354 flagword applicable;
355 #endif
356
357 need_pass_2 = 0;
358
359 #ifndef BFD_ASSEMBLER
360 #ifdef MANY_SEGMENTS
361 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
362 {
363 segment_info[i].fix_root = 0;
364 }
365 /* Create the three fixed ones */
366 subseg_new (SEG_E0, 0);
367 subseg_new (SEG_E1, 0);
368 subseg_new (SEG_E2, 0);
369 strcpy (segment_info[SEG_E0].scnhdr.s_name, ".text");
370 strcpy (segment_info[SEG_E1].scnhdr.s_name, ".data");
371 strcpy (segment_info[SEG_E2].scnhdr.s_name, ".bss");
372
373 subseg_new (SEG_E0, 0);
374 #else /* not MANY_SEGMENTS */
375 text_fix_root = NULL;
376 data_fix_root = NULL;
377 bss_fix_root = NULL;
378
379 subseg_new (SEG_TEXT, 0);
380 #endif /* not MANY_SEGMENTS */
381 #else /* BFD_ASSEMBLER */
382 /* Create the standard sections, and those the assembler uses
383 internally. */
384 text_section = subseg_new (".text", 0);
385 text_section->output_section = text_section;
386 data_section = subseg_new (".data", 0);
387 data_section->output_section = data_section;
388 bss_section = subseg_new (".bss", 0);
389 bss_section->output_section = bss_section;
390 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
391 to have relocs, otherwise we don't find out in time. */
392 applicable = bfd_applicable_section_flags (stdoutput);
393 bfd_set_section_flags (stdoutput, text_section,
394 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
395 | SEC_CODE | SEC_READONLY));
396 /* @@ FIXME -- SEC_CODE seems to mean code only, rather than code possibly.*/
397 bfd_set_section_flags (stdoutput, data_section,
398 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC));
399 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
400 subseg_new (BFD_ABS_SECTION_NAME, 0);
401 subseg_new (BFD_UND_SECTION_NAME, 0);
402 big_section = subseg_new ("*GAS `big' section*", 0);
403 reg_section = subseg_new ("*GAS `reg' section*", 0);
404 pass1_section = subseg_new ("*GAS `pass1' section*", 0);
405 diff_section = subseg_new ("*GAS `diff' section*", 0);
406 absent_section = subseg_new ("*GAS `absent' section*", 0);
407
408 subseg_new (".text", 0);
409 #endif /* BFD_ASSEMBLER */
410
411 argv++; /* skip argv[0] */
412 argc--; /* skip argv[0] */
413 while (argc--)
414 {
415 if (*argv)
416 { /* Is it a file-name argument? */
417 saw_a_file++;
418 /* argv->"" if stdin desired, else->filename */
419 read_a_source_file (*argv);
420 }
421 argv++; /* completed that argv */
422 }
423 if (!saw_a_file)
424 read_a_source_file ("");
425 } /* perform_an_assembly_pass() */
426 \f
427 /*
428 * stralloc()
429 *
430 * Allocate memory for a new copy of a string. Copy the string.
431 * Return the address of the new string. Die if there is any error.
432 */
433
434 static char *
435 stralloc (str)
436 char *str;
437 {
438 register char *retval;
439 register long len;
440
441 len = strlen (str) + 1;
442 retval = xmalloc (len);
443 (void) strcpy (retval, str);
444 return (retval);
445 }
446 \f
447 static SIGTY
448 got_sig (sig)
449 int sig;
450 {
451 static here_before = 0;
452
453 as_bad ("Interrupted by signal %d", sig);
454 if (here_before++)
455 exit (1);
456 return ((SIGTY) 0);
457 }
458
459 /* end of as.c */