xref: /csrg-svn/contrib/gas-1.38/as.c (revision 47427)
1*47427Sdonn /* as.c - GAS main program.
2*47427Sdonn    Copyright (C) 1987 Free Software Foundation, Inc.
3*47427Sdonn 
4*47427Sdonn This file is part of GAS, the GNU Assembler.
5*47427Sdonn 
6*47427Sdonn GAS is free software; you can redistribute it and/or modify
7*47427Sdonn it under the terms of the GNU General Public License as published by
8*47427Sdonn the Free Software Foundation; either version 1, or (at your option)
9*47427Sdonn any later version.
10*47427Sdonn 
11*47427Sdonn GAS is distributed in the hope that it will be useful,
12*47427Sdonn but WITHOUT ANY WARRANTY; without even the implied warranty of
13*47427Sdonn MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*47427Sdonn GNU General Public License for more details.
15*47427Sdonn 
16*47427Sdonn You should have received a copy of the GNU General Public License
17*47427Sdonn along with GAS; see the file COPYING.  If not, write to
18*47427Sdonn the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19*47427Sdonn 
20*47427Sdonn /*
21*47427Sdonn  * Main program for AS; a 32-bit assembler of GNU.
22*47427Sdonn  * Understands command arguments.
23*47427Sdonn  * Has a few routines that don't fit in other modules because they
24*47427Sdonn  * are shared.
25*47427Sdonn  *
26*47427Sdonn  *
27*47427Sdonn  *			bugs
28*47427Sdonn  *
29*47427Sdonn  * : initialisers
30*47427Sdonn  *	Since no-one else says they will support them in future: I
31*47427Sdonn  * don't support them now.
32*47427Sdonn  *
33*47427Sdonn  */
34*47427Sdonn 
35*47427Sdonn #ifdef _POSIX_SOURCE
36*47427Sdonn #include <sys/types.h>	/* For pid_t in signal.h */
37*47427Sdonn #endif
38*47427Sdonn #include <signal.h>
39*47427Sdonn 
40*47427Sdonn #define COMMON
41*47427Sdonn #include "as.h"
42*47427Sdonn #include "struc-symbol.h"
43*47427Sdonn #include "write.h"
44*47427Sdonn 		/* Warning!  This may have some slightly strange side effects
45*47427Sdonn 		   if you try to compile two or more assemblers in the same
46*47427Sdonn 		   directory!
47*47427Sdonn 		 */
48*47427Sdonn 
49*47427Sdonn #ifndef SIGTY
50*47427Sdonn #define SIGTY int
51*47427Sdonn #endif
52*47427Sdonn 
53*47427Sdonn SIGTY got_sig();
54*47427Sdonn 
55*47427Sdonn #ifdef DONTDEF
56*47427Sdonn static char * gdb_symbol_file_name;
57*47427Sdonn long int gdb_begin();
58*47427Sdonn #endif
59*47427Sdonn 
60*47427Sdonn char *myname;		/* argv[0] */
61*47427Sdonn extern char version_string[];
62*47427Sdonn 
63*47427Sdonn main(argc,argv)
64*47427Sdonn int	argc;
65*47427Sdonn char	**argv;
66*47427Sdonn {
67*47427Sdonn 	int	work_argc;	/* variable copy of argc */
68*47427Sdonn 	char	**work_argv;	/* variable copy of argv */
69*47427Sdonn 	char	*arg;		/* an arg to program */
70*47427Sdonn 	char	a;		/* an arg flag (after -) */
71*47427Sdonn 	static const int sig[] = { SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
72*47427Sdonn 
73*47427Sdonn 	extern int bad_error;	/* Did we hit a bad error ? */
74*47427Sdonn 
75*47427Sdonn 	char	*stralloc();	/* Make a (safe) copy of a string. */
76*47427Sdonn 	void	symbol_begin();
77*47427Sdonn 	void	read_begin();
78*47427Sdonn 	void	write_object_file();
79*47427Sdonn 
80*47427Sdonn 	for(a=0;sig[a]!=0;a++)
81*47427Sdonn 		if(signal(sig[a], SIG_IGN) != SIG_IGN)
82*47427Sdonn 			signal(sig[a], got_sig);
83*47427Sdonn 
84*47427Sdonn 	myname=argv[0];
85*47427Sdonn 	bzero (flagseen, sizeof(flagseen)); /* aint seen nothing yet */
86*47427Sdonn 	out_file_name	= "a.out";	/* default .o file */
87*47427Sdonn 	symbol_begin();		/* symbols.c */
88*47427Sdonn 	subsegs_begin();		/* subsegs.c */
89*47427Sdonn 	read_begin();			/* read.c */
90*47427Sdonn 	md_begin();			/* MACHINE.c */
91*47427Sdonn 	input_scrub_begin();		/* input_scrub.c */
92*47427Sdonn #ifdef DONTDEF
93*47427Sdonn 	gdb_symbol_file_name = 0;
94*47427Sdonn #endif
95*47427Sdonn 	/*
96*47427Sdonn 	 * Parse arguments, but we are only interested in flags.
97*47427Sdonn 	 * When we find a flag, we process it then make it's argv[] NULL.
98*47427Sdonn 	 * This helps any future argv[] scanners avoid what we processed.
99*47427Sdonn 	 * Since it is easy to do here we interpret the special arg "-"
100*47427Sdonn 	 * to mean "use stdin" and we set that argv[] pointing to "".
101*47427Sdonn 	 * After we have munged argv[], the only things left are source file
102*47427Sdonn 	 * name(s) and ""(s) denoting stdin. These file names are used
103*47427Sdonn 	 * (perhaps more than once) later.
104*47427Sdonn 	 */
105*47427Sdonn 	work_argc = argc-1;		/* don't count argv[0] */
106*47427Sdonn 	work_argv = argv+1;		/* skip argv[0] */
107*47427Sdonn 	for (;work_argc--;work_argv++) {
108*47427Sdonn 		arg = * work_argv;	/* work_argv points to this argument */
109*47427Sdonn 
110*47427Sdonn 		if (*arg!='-')		/* Filename. We need it later. */
111*47427Sdonn 			continue;	/* Keep scanning args looking for flags. */
112*47427Sdonn 		if (arg[1] == '-' && arg[2] == 0) {
113*47427Sdonn 			/* "--" as an argument means read STDIN */
114*47427Sdonn 			/* on this scan, we don't want to think about filenames */
115*47427Sdonn 			* work_argv = "";	/* Code that means 'use stdin'. */
116*47427Sdonn 			continue;
117*47427Sdonn 		}
118*47427Sdonn 				/* This better be a switch. */
119*47427Sdonn 		arg ++;		/* -> letter. */
120*47427Sdonn 
121*47427Sdonn 		while (a = * arg)  {/* scan all the 1-char flags */
122*47427Sdonn 			arg ++;	/* arg -> after letter. */
123*47427Sdonn 			a &= 0x7F;	/* ascii only please */
124*47427Sdonn 			if (flagseen[a])
125*47427Sdonn 				as_warn("%s: Flag option -%c has already been seen!",myname,a);
126*47427Sdonn 			flagseen[a] = TRUE;
127*47427Sdonn 			switch (a) {
128*47427Sdonn 			case 'f':
129*47427Sdonn 				break;	/* -f means fast - no need for "app" preprocessor. */
130*47427Sdonn 
131*47427Sdonn 			case 'D':
132*47427Sdonn 				/* DEBUG is implemented: it debugs different */
133*47427Sdonn 				/* things to other people's assemblers. */
134*47427Sdonn 				break;
135*47427Sdonn 
136*47427Sdonn #ifdef DONTDEF
137*47427Sdonn 			case 'G':	/* GNU AS switch: include gdbsyms. */
138*47427Sdonn 				if (*arg)	/* Rest of argument is file-name. */
139*47427Sdonn 					gdb_symbol_file_name = stralloc (arg);
140*47427Sdonn 				else if (work_argc) {	/* Next argument is file-name. */
141*47427Sdonn 					work_argc --;
142*47427Sdonn 					* work_argv = NULL; /* Not a source file-name. */
143*47427Sdonn 					gdb_symbol_file_name = * ++ work_argv;
144*47427Sdonn 				} else
145*47427Sdonn 					as_warn( "%s: I expected a filename after -G",myname);
146*47427Sdonn 				arg = "";	/* Finished with this arg. */
147*47427Sdonn 				break;
148*47427Sdonn #endif
149*47427Sdonn 
150*47427Sdonn #ifndef WORKING_DOT_WORD
151*47427Sdonn 			case 'k':
152*47427Sdonn 				break;
153*47427Sdonn #endif
154*47427Sdonn 
155*47427Sdonn 			case 'L': /* -L means keep L* symbols */
156*47427Sdonn 				break;
157*47427Sdonn 
158*47427Sdonn 			case 'o':
159*47427Sdonn 				if (*arg)	/* Rest of argument is object file-name. */
160*47427Sdonn 					out_file_name = stralloc (arg);
161*47427Sdonn 				else if (work_argc) {	/* Want next arg for a file-name. */
162*47427Sdonn 					* work_argv = NULL; /* This is not a file-name. */
163*47427Sdonn 					work_argc--;
164*47427Sdonn 					out_file_name = * ++ work_argv;
165*47427Sdonn 				} else
166*47427Sdonn 					as_warn("%s: I expected a filename after -o. \"%s\" assumed.",myname,out_file_name);
167*47427Sdonn 				arg = "";	/* Finished with this arg. */
168*47427Sdonn 				break;
169*47427Sdonn 
170*47427Sdonn 			case 'R':
171*47427Sdonn 				/* -R means put data into text segment */
172*47427Sdonn 				break;
173*47427Sdonn 
174*47427Sdonn 			case 'v':
175*47427Sdonn #ifdef	VMS
176*47427Sdonn 				{
177*47427Sdonn 				extern char *compiler_version_string;
178*47427Sdonn 				compiler_version_string = arg;
179*47427Sdonn 				}
180*47427Sdonn #else /* not VMS */
181*47427Sdonn 				fprintf(stderr,version_string);
182*47427Sdonn 				if(*arg && strcmp(arg,"ersion"))
183*47427Sdonn 					as_warn("Unknown -v option ignored");
184*47427Sdonn #endif
185*47427Sdonn 				while(*arg) arg++;	/* Skip the rest */
186*47427Sdonn 				break;
187*47427Sdonn 
188*47427Sdonn 			case 'W':
189*47427Sdonn 				/* -W means don't warn about things */
190*47427Sdonn 				break;
191*47427Sdonn 
192*47427Sdonn 			default:
193*47427Sdonn 				--arg;
194*47427Sdonn 				if(md_parse_option(&arg,&work_argc,&work_argv)==0)
195*47427Sdonn 					as_warn("%s: I don't understand '%c' flag!",myname,a);
196*47427Sdonn 				if(arg && *arg)
197*47427Sdonn 					arg++;
198*47427Sdonn 				break;
199*47427Sdonn 			}
200*47427Sdonn 		}
201*47427Sdonn 		/*
202*47427Sdonn 		 * We have just processed a "-..." arg, which was not a
203*47427Sdonn 		 * file-name. Smash it so the
204*47427Sdonn 		 * things that look for filenames won't ever see it.
205*47427Sdonn 		 *
206*47427Sdonn 		 * Whatever work_argv points to, it has already been used
207*47427Sdonn 		 * as part of a flag, so DON'T re-use it as a filename.
208*47427Sdonn 		 */
209*47427Sdonn 		*work_argv = NULL; /* NULL means 'not a file-name' */
210*47427Sdonn 	}
211*47427Sdonn #ifdef DONTDEF
212*47427Sdonn 	if (gdb_begin(gdb_symbol_file_name) == 0)
213*47427Sdonn 		flagseen ['G'] = 0;	/* Don't do any gdbsym stuff. */
214*47427Sdonn #endif
215*47427Sdonn 	/* Here with flags set up in flagseen[]. */
216*47427Sdonn 	perform_an_assembly_pass(argc,argv); /* Assemble it. */
217*47427Sdonn 	if (seen_at_least_1_file() && !bad_error)
218*47427Sdonn 		write_object_file();/* relax() addresses then emit object file */
219*47427Sdonn 	input_scrub_end();
220*47427Sdonn 	md_end();			/* MACHINE.c */
221*47427Sdonn #ifndef	VMS
222*47427Sdonn 	exit(bad_error);			/* WIN */
223*47427Sdonn #else	/* VMS */
224*47427Sdonn 	exit(!bad_error);			/* WIN */
225*47427Sdonn #endif	/* VMS */
226*47427Sdonn }
227*47427Sdonn 
228*47427Sdonn 
229*47427Sdonn /*			perform_an_assembly_pass()
230*47427Sdonn  *
231*47427Sdonn  * Here to attempt 1 pass over each input file.
232*47427Sdonn  * We scan argv[*] looking for filenames or exactly "" which is
233*47427Sdonn  * shorthand for stdin. Any argv that is NULL is not a file-name.
234*47427Sdonn  * We set need_pass_2 TRUE if, after this, we still have unresolved
235*47427Sdonn  * expressions of the form (unknown value)+-(unknown value).
236*47427Sdonn  *
237*47427Sdonn  * Note the un*x semantics: there is only 1 logical input file, but it
238*47427Sdonn  * may be a catenation of many 'physical' input files.
239*47427Sdonn  */
240*47427Sdonn perform_an_assembly_pass (argc, argv)
241*47427Sdonn int	argc;
242*47427Sdonn char **	argv;
243*47427Sdonn {
244*47427Sdonn 	char *	buffer;		/* Where each bufferful of lines will start. */
245*47427Sdonn 	void	read_a_source_file();
246*47427Sdonn 	int saw_a_file = 0;
247*47427Sdonn 
248*47427Sdonn 	text_fix_root		= NULL;
249*47427Sdonn 	data_fix_root		= NULL;
250*47427Sdonn 	need_pass_2		= FALSE;
251*47427Sdonn 
252*47427Sdonn 	argv++;			/* skip argv[0] */
253*47427Sdonn 	argc--;			/* skip argv[0] */
254*47427Sdonn 	while (argc--) {
255*47427Sdonn 		if (*argv) {		/* Is it a file-name argument? */
256*47427Sdonn 			/* argv -> "" if stdin desired, else -> filename */
257*47427Sdonn 			if (buffer = input_scrub_new_file (*argv) ) {
258*47427Sdonn 				saw_a_file++;
259*47427Sdonn 				read_a_source_file(buffer);
260*47427Sdonn 			}
261*47427Sdonn 		}
262*47427Sdonn 		argv++;			/* completed that argv */
263*47427Sdonn 	}
264*47427Sdonn 	if(!saw_a_file)
265*47427Sdonn 		if(buffer = input_scrub_new_file("") )
266*47427Sdonn 			read_a_source_file(buffer);
267*47427Sdonn }
268*47427Sdonn 
269*47427Sdonn /*
270*47427Sdonn  *			stralloc()
271*47427Sdonn  *
272*47427Sdonn  * Allocate memory for a new copy of a string. Copy the string.
273*47427Sdonn  * Return the address of the new string. Die if there is any error.
274*47427Sdonn  */
275*47427Sdonn 
276*47427Sdonn char *
277*47427Sdonn stralloc (str)
278*47427Sdonn char *	str;
279*47427Sdonn {
280*47427Sdonn 	register char *	retval;
281*47427Sdonn 	register long int	len;
282*47427Sdonn 
283*47427Sdonn 	len = strlen (str) + 1;
284*47427Sdonn 	retval = xmalloc (len);
285*47427Sdonn 	(void)strcpy (retval, str);
286*47427Sdonn 	return (retval);
287*47427Sdonn }
288*47427Sdonn 
289*47427Sdonn lose()
290*47427Sdonn {
291*47427Sdonn 	as_fatal( "%s: 2nd pass not implemented - get your code from random(3)",myname );
292*47427Sdonn }
293*47427Sdonn 
294*47427Sdonn SIGTY
295*47427Sdonn got_sig(sig)
296*47427Sdonn int sig;
297*47427Sdonn {
298*47427Sdonn 	static here_before = 0;
299*47427Sdonn 
300*47427Sdonn 	as_bad("Interrupted by signal %d",sig);
301*47427Sdonn 	if(here_before++)
302*47427Sdonn 		exit(1);
303*47427Sdonn }
304*47427Sdonn 
305*47427Sdonn /* end: as.c */
306