* write.c (write_contents): Compute the relocs before writing out
[deliverable/binutils-gdb.git] / gas / as.c
CommitLineData
fecd2382 1/* as.c - GAS main program.
3340f7e5 2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
6efd877d 3
a39116f1 4 This file is part of GAS, the GNU Assembler.
6efd877d 5
a39116f1
RP
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.
6efd877d 10
a39116f1
RP
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.
6efd877d 15
a39116f1
RP
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. */
fecd2382
RP
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
6efd877d 39#include <sys/types.h> /* For pid_t in signal.h */
fecd2382
RP
40#endif
41#include <signal.h>
42
43#define COMMON
44
45#include "as.h"
5d9f0ecf 46#include "subsegs.h"
3340f7e5 47#if __STDC__ == 1
fecd2382 48
a39116f1
RP
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. */
fecd2382
RP
54
55#define SIGTY void
56
6efd877d
KR
57static void got_sig (int sig);
58static char *stralloc (char *str);
59static void perform_an_assembly_pass (int argc, char **argv);
fecd2382
RP
60
61#else /* __STDC__ */
62
63#ifndef SIGTY
64#define SIGTY int
65#endif
66
6efd877d
KR
67static SIGTY got_sig ();
68static char *stralloc (); /* Make a (safe) copy of a string. */
69static void perform_an_assembly_pass ();
fecd2382 70
3340f7e5 71#endif /* not __STDC__ */
fecd2382 72
6efd877d 73int listing; /* true if a listing is wanted */
5d9f0ecf 74
6efd877d 75char *myname; /* argv[0] */
fecd2382 76extern char version_string[];
67f3dd71
KR
77#ifdef BFD_ASSEMBLER
78segT big_section, reg_section, pass1_section;
79segT diff_section, absent_section;
80segT text_section, data_section, bss_section;
81#endif
fecd2382 82\f
6efd877d
KR
83int
84main (argc, argv)
85 int argc;
86 char **argv;
fecd2382 87{
6efd877d
KR
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 -) */
67f3dd71 92 static const int sig[] = {SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
6efd877d
KR
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 */
fecd2382
RP
100#ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
101#define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
102#endif /* OBJ_DEFAULT_OUTPUT_FILE_NAME */
6efd877d
KR
103 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
104
67f3dd71
KR
105#ifdef BFD_ASSEMBLER
106 bfd_init ();
107#endif
108
6efd877d
KR
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 */
6efd877d 114 /*
67f3dd71
KR
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 */
6efd877d
KR
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 */
6efd877d
KR
147 flagseen[a] = 1;
148 switch (a)
149 {
6efd877d
KR
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
6efd877d
KR
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;
fecd2382 222#endif
6efd877d 223
fecd2382 224#ifndef WORKING_DOT_WORD
6efd877d
KR
225 case 'K':
226 break;
fecd2382 227#endif
6efd877d
KR
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 */
67f3dd71 248 flag_readonly_data_in_text = 1;
6efd877d
KR
249 break;
250
251 case 'v':
fecd2382 252#ifdef VMS
6efd877d
KR
253 {
254 extern char *compiler_version_string;
255 compiler_version_string = arg;
256 }
fecd2382 257#else /* not VMS */
6efd877d
KR
258 fprintf (stderr, version_string);
259 if (*arg && strcmp (arg, "ersion"))
260 as_warn ("Unknown -v option ignored");
fecd2382 261#endif
6efd877d
KR
262 while (*arg)
263 arg++; /* Skip the rest */
264 break;
265
266 case 'W':
6efd877d 267 /* -W means don't warn about things */
67f3dd71
KR
268 flag_suppress_warnings = 1;
269 break;
270
271 case 'w':
6efd877d
KR
272 case 'X':
273 /* -X means treat warnings as errors */
67f3dd71 274 break;
6efd877d
KR
275 case 'Z':
276 /* -Z means attempt to generate object file even after errors. */
67f3dd71 277 flag_always_generate_output = 1;
6efd877d
KR
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 /*
67f3dd71
KR
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 */
6efd877d
KR
297 *work_argv = NULL; /* NULL means 'not a file-name' */
298 }
67f3dd71
KR
299
300#ifdef BFD_ASSEMBLER
301 output_file_create (out_file_name);
302 assert (stdoutput != 0);
fecd2382 303#endif
67f3dd71 304
6efd877d
KR
305 /* Here with flags set up in flagseen[]. */
306 perform_an_assembly_pass (argc, argv); /* Assemble it. */
fecd2382 307#ifdef TC_I960
6efd877d 308 brtab_emit ();
fecd2382 309#endif
6efd877d 310 if (seen_at_least_1_file ()
67f3dd71 311 && !((had_warnings () && flag_always_generate_output)
6efd877d
KR
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
5d9f0ecf 320#ifndef NO_LISTING
6efd877d 321 listing_print ("");
5d9f0ecf 322#endif
6efd877d 323
fecd2382 324#ifndef VMS
6efd877d
KR
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 */
fecd2382 331
6efd877d 332} /* main() */
fecd2382 333\f
6efd877d 334
fecd2382
RP
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 */
6efd877d
KR
346static void
347perform_an_assembly_pass (argc, argv)
348 int argc;
349 char **argv;
fecd2382 350{
6efd877d
KR
351 int saw_a_file = 0;
352 unsigned int i;
67f3dd71
KR
353#ifdef BFD_ASSEMBLER
354 flagword applicable;
355#endif
356
6efd877d
KR
357 need_pass_2 = 0;
358
67f3dd71 359#ifndef BFD_ASSEMBLER
5d9f0ecf 360#ifdef MANY_SEGMENTS
6efd877d
KR
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);
e6498b10 374#else /* not MANY_SEGMENTS */
6efd877d
KR
375 text_fix_root = NULL;
376 data_fix_root = NULL;
377 bss_fix_root = NULL;
378
379 subseg_new (SEG_TEXT, 0);
e6498b10 380#endif /* not MANY_SEGMENTS */
67f3dd71
KR
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 */
e6498b10 410
6efd877d
KR
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);
a39116f1 420 }
6efd877d
KR
421 argv++; /* completed that argv */
422 }
423 if (!saw_a_file)
424 read_a_source_file ("");
425} /* perform_an_assembly_pass() */
fecd2382
RP
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
434static char *
6efd877d
KR
435stralloc (str)
436 char *str;
fecd2382 437{
6efd877d
KR
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);
fecd2382
RP
445}
446\f
fecd2382 447static SIGTY
6efd877d
KR
448got_sig (sig)
449 int sig;
fecd2382 450{
6efd877d
KR
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);
fecd2382
RP
457}
458
a39116f1 459/* end of as.c */
This page took 0.081834 seconds and 4 git commands to generate.