xref: /csrg-svn/contrib/gas-1.38/as.c (revision 49403)
1*49403Sbostic /*-
2*49403Sbostic  * This code is derived from software copyrighted by the Free Software
3*49403Sbostic  * Foundation.
4*49403Sbostic  *
5*49403Sbostic  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6*49403Sbostic  */
747442Sdonn 
8*49403Sbostic #ifndef lint
9*49403Sbostic static char sccsid[] = "@(#)as.c	6.3 (Berkeley) 05/08/91";
10*49403Sbostic #endif /* not lint */
1147442Sdonn 
1247427Sdonn /* as.c - GAS main program.
1347427Sdonn    Copyright (C) 1987 Free Software Foundation, Inc.
1447427Sdonn 
1547427Sdonn This file is part of GAS, the GNU Assembler.
1647427Sdonn 
1747427Sdonn GAS is free software; you can redistribute it and/or modify
1847427Sdonn it under the terms of the GNU General Public License as published by
1947427Sdonn the Free Software Foundation; either version 1, or (at your option)
2047427Sdonn any later version.
2147427Sdonn 
2247427Sdonn GAS is distributed in the hope that it will be useful,
2347427Sdonn but WITHOUT ANY WARRANTY; without even the implied warranty of
2447427Sdonn MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2547427Sdonn GNU General Public License for more details.
2647427Sdonn 
2747427Sdonn You should have received a copy of the GNU General Public License
2847427Sdonn along with GAS; see the file COPYING.  If not, write to
2947427Sdonn the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
3047427Sdonn 
3147427Sdonn /*
3247427Sdonn  * Main program for AS; a 32-bit assembler of GNU.
3347427Sdonn  * Understands command arguments.
3447427Sdonn  * Has a few routines that don't fit in other modules because they
3547427Sdonn  * are shared.
3647427Sdonn  *
3747427Sdonn  *
3847427Sdonn  *			bugs
3947427Sdonn  *
4047427Sdonn  * : initialisers
4147427Sdonn  *	Since no-one else says they will support them in future: I
4247427Sdonn  * don't support them now.
4347427Sdonn  *
4447427Sdonn  */
4547427Sdonn 
4647427Sdonn #ifdef _POSIX_SOURCE
4747427Sdonn #include <sys/types.h>	/* For pid_t in signal.h */
4847427Sdonn #endif
4947427Sdonn #include <signal.h>
5047427Sdonn 
5147427Sdonn #define COMMON
5247427Sdonn #include "as.h"
5347427Sdonn #include "struc-symbol.h"
5447427Sdonn #include "write.h"
5547427Sdonn 		/* Warning!  This may have some slightly strange side effects
5647427Sdonn 		   if you try to compile two or more assemblers in the same
5747427Sdonn 		   directory!
5847427Sdonn 		 */
5947427Sdonn 
6047427Sdonn #ifndef SIGTY
6147427Sdonn #define SIGTY int
6247427Sdonn #endif
6347427Sdonn 
6447427Sdonn SIGTY got_sig();
6547427Sdonn 
6647427Sdonn #ifdef DONTDEF
6747427Sdonn static char * gdb_symbol_file_name;
6847427Sdonn long int gdb_begin();
6947427Sdonn #endif
7047427Sdonn 
7147427Sdonn char *myname;		/* argv[0] */
7247427Sdonn extern char version_string[];
7347427Sdonn 
main(argc,argv)7447427Sdonn main(argc,argv)
7547427Sdonn int	argc;
7647427Sdonn char	**argv;
7747427Sdonn {
7847427Sdonn 	int	work_argc;	/* variable copy of argc */
7947427Sdonn 	char	**work_argv;	/* variable copy of argv */
8047427Sdonn 	char	*arg;		/* an arg to program */
8147427Sdonn 	char	a;		/* an arg flag (after -) */
8247427Sdonn 	static const int sig[] = { SIGHUP, SIGINT, SIGPIPE, SIGTERM, 0};
8347427Sdonn 
8447427Sdonn 	extern int bad_error;	/* Did we hit a bad error ? */
8547427Sdonn 
8647427Sdonn 	char	*stralloc();	/* Make a (safe) copy of a string. */
8747427Sdonn 	void	symbol_begin();
8847427Sdonn 	void	read_begin();
8947427Sdonn 	void	write_object_file();
9047427Sdonn 
9147427Sdonn 	for(a=0;sig[a]!=0;a++)
9247427Sdonn 		if(signal(sig[a], SIG_IGN) != SIG_IGN)
9347427Sdonn 			signal(sig[a], got_sig);
9447427Sdonn 
9547427Sdonn 	myname=argv[0];
9647427Sdonn 	bzero (flagseen, sizeof(flagseen)); /* aint seen nothing yet */
9747427Sdonn 	out_file_name	= "a.out";	/* default .o file */
9847427Sdonn 	symbol_begin();		/* symbols.c */
9947427Sdonn 	subsegs_begin();		/* subsegs.c */
10047427Sdonn 	read_begin();			/* read.c */
10147427Sdonn 	md_begin();			/* MACHINE.c */
10247427Sdonn 	input_scrub_begin();		/* input_scrub.c */
10347427Sdonn #ifdef DONTDEF
10447427Sdonn 	gdb_symbol_file_name = 0;
10547427Sdonn #endif
10647427Sdonn 	/*
10747427Sdonn 	 * Parse arguments, but we are only interested in flags.
10847427Sdonn 	 * When we find a flag, we process it then make it's argv[] NULL.
10947427Sdonn 	 * This helps any future argv[] scanners avoid what we processed.
11047427Sdonn 	 * Since it is easy to do here we interpret the special arg "-"
11147427Sdonn 	 * to mean "use stdin" and we set that argv[] pointing to "".
11247427Sdonn 	 * After we have munged argv[], the only things left are source file
11347427Sdonn 	 * name(s) and ""(s) denoting stdin. These file names are used
11447427Sdonn 	 * (perhaps more than once) later.
11547427Sdonn 	 */
11647427Sdonn 	work_argc = argc-1;		/* don't count argv[0] */
11747427Sdonn 	work_argv = argv+1;		/* skip argv[0] */
11847427Sdonn 	for (;work_argc--;work_argv++) {
11947427Sdonn 		arg = * work_argv;	/* work_argv points to this argument */
12047427Sdonn 
12147427Sdonn 		if (*arg!='-')		/* Filename. We need it later. */
12247427Sdonn 			continue;	/* Keep scanning args looking for flags. */
12347427Sdonn 		if (arg[1] == '-' && arg[2] == 0) {
12447427Sdonn 			/* "--" as an argument means read STDIN */
12547427Sdonn 			/* on this scan, we don't want to think about filenames */
12647427Sdonn 			* work_argv = "";	/* Code that means 'use stdin'. */
12747427Sdonn 			continue;
12847427Sdonn 		}
12947427Sdonn 				/* This better be a switch. */
13047427Sdonn 		arg ++;		/* -> letter. */
13147427Sdonn 
13247427Sdonn 		while (a = * arg)  {/* scan all the 1-char flags */
13347427Sdonn 			arg ++;	/* arg -> after letter. */
13447427Sdonn 			a &= 0x7F;	/* ascii only please */
13547427Sdonn 			if (flagseen[a])
13647427Sdonn 				as_warn("%s: Flag option -%c has already been seen!",myname,a);
13747427Sdonn 			flagseen[a] = TRUE;
13847427Sdonn 			switch (a) {
13947427Sdonn 			case 'f':
14047427Sdonn 				break;	/* -f means fast - no need for "app" preprocessor. */
14147427Sdonn 
14247427Sdonn 			case 'D':
14347427Sdonn 				/* DEBUG is implemented: it debugs different */
14447427Sdonn 				/* things to other people's assemblers. */
14547427Sdonn 				break;
14647427Sdonn 
14747427Sdonn #ifdef DONTDEF
14847427Sdonn 			case 'G':	/* GNU AS switch: include gdbsyms. */
14947427Sdonn 				if (*arg)	/* Rest of argument is file-name. */
15047427Sdonn 					gdb_symbol_file_name = stralloc (arg);
15147427Sdonn 				else if (work_argc) {	/* Next argument is file-name. */
15247427Sdonn 					work_argc --;
15347427Sdonn 					* work_argv = NULL; /* Not a source file-name. */
15447427Sdonn 					gdb_symbol_file_name = * ++ work_argv;
15547427Sdonn 				} else
15647427Sdonn 					as_warn( "%s: I expected a filename after -G",myname);
15747427Sdonn 				arg = "";	/* Finished with this arg. */
15847427Sdonn 				break;
15947427Sdonn #endif
16047427Sdonn 
16147427Sdonn #ifndef WORKING_DOT_WORD
16247427Sdonn 			case 'k':
16347427Sdonn 				break;
16447427Sdonn #endif
16547427Sdonn 
16647427Sdonn 			case 'L': /* -L means keep L* symbols */
16747427Sdonn 				break;
16847427Sdonn 
16947427Sdonn 			case 'o':
17047427Sdonn 				if (*arg)	/* Rest of argument is object file-name. */
17147427Sdonn 					out_file_name = stralloc (arg);
17247427Sdonn 				else if (work_argc) {	/* Want next arg for a file-name. */
17347427Sdonn 					* work_argv = NULL; /* This is not a file-name. */
17447427Sdonn 					work_argc--;
17547427Sdonn 					out_file_name = * ++ work_argv;
17647427Sdonn 				} else
17747427Sdonn 					as_warn("%s: I expected a filename after -o. \"%s\" assumed.",myname,out_file_name);
17847427Sdonn 				arg = "";	/* Finished with this arg. */
17947427Sdonn 				break;
18047427Sdonn 
18147427Sdonn 			case 'R':
18247427Sdonn 				/* -R means put data into text segment */
18347427Sdonn 				break;
18447427Sdonn 
18547427Sdonn 			case 'v':
18647427Sdonn #ifdef	VMS
18747427Sdonn 				{
18847427Sdonn 				extern char *compiler_version_string;
18947427Sdonn 				compiler_version_string = arg;
19047427Sdonn 				}
19147427Sdonn #else /* not VMS */
19247427Sdonn 				fprintf(stderr,version_string);
19347427Sdonn 				if(*arg && strcmp(arg,"ersion"))
19447427Sdonn 					as_warn("Unknown -v option ignored");
19547427Sdonn #endif
19647427Sdonn 				while(*arg) arg++;	/* Skip the rest */
19747427Sdonn 				break;
19847427Sdonn 
19947427Sdonn 			case 'W':
20047427Sdonn 				/* -W means don't warn about things */
20147427Sdonn 				break;
20247427Sdonn 
20347442Sdonn 			case 'g':
20447442Sdonn 				/*
20547442Sdonn 				 * -g asks gas to produce gdb/dbx line number
20647442Sdonn 				 * and file name stabs so that an assembly
20747442Sdonn 				 * file can be handled by a source debugger.
20847442Sdonn 				 */
20947442Sdonn 				break;
21047442Sdonn 
21147427Sdonn 			default:
21247427Sdonn 				--arg;
21347427Sdonn 				if(md_parse_option(&arg,&work_argc,&work_argv)==0)
21447427Sdonn 					as_warn("%s: I don't understand '%c' flag!",myname,a);
21547427Sdonn 				if(arg && *arg)
21647427Sdonn 					arg++;
21747427Sdonn 				break;
21847427Sdonn 			}
21947427Sdonn 		}
22047427Sdonn 		/*
22147427Sdonn 		 * We have just processed a "-..." arg, which was not a
22247427Sdonn 		 * file-name. Smash it so the
22347427Sdonn 		 * things that look for filenames won't ever see it.
22447427Sdonn 		 *
22547427Sdonn 		 * Whatever work_argv points to, it has already been used
22647427Sdonn 		 * as part of a flag, so DON'T re-use it as a filename.
22747427Sdonn 		 */
22847427Sdonn 		*work_argv = NULL; /* NULL means 'not a file-name' */
22947427Sdonn 	}
23047427Sdonn #ifdef DONTDEF
23147427Sdonn 	if (gdb_begin(gdb_symbol_file_name) == 0)
23247427Sdonn 		flagseen ['G'] = 0;	/* Don't do any gdbsym stuff. */
23347427Sdonn #endif
23447427Sdonn 	/* Here with flags set up in flagseen[]. */
23547427Sdonn 	perform_an_assembly_pass(argc,argv); /* Assemble it. */
23647427Sdonn 	if (seen_at_least_1_file() && !bad_error)
23747427Sdonn 		write_object_file();/* relax() addresses then emit object file */
23847427Sdonn 	input_scrub_end();
23947427Sdonn 	md_end();			/* MACHINE.c */
24047427Sdonn #ifndef	VMS
24147427Sdonn 	exit(bad_error);			/* WIN */
24247427Sdonn #else	/* VMS */
24347427Sdonn 	exit(!bad_error);			/* WIN */
24447427Sdonn #endif	/* VMS */
24547427Sdonn }
24647427Sdonn 
24747427Sdonn 
24847427Sdonn /*			perform_an_assembly_pass()
24947427Sdonn  *
25047427Sdonn  * Here to attempt 1 pass over each input file.
25147427Sdonn  * We scan argv[*] looking for filenames or exactly "" which is
25247427Sdonn  * shorthand for stdin. Any argv that is NULL is not a file-name.
25347427Sdonn  * We set need_pass_2 TRUE if, after this, we still have unresolved
25447427Sdonn  * expressions of the form (unknown value)+-(unknown value).
25547427Sdonn  *
25647427Sdonn  * Note the un*x semantics: there is only 1 logical input file, but it
25747427Sdonn  * may be a catenation of many 'physical' input files.
25847427Sdonn  */
perform_an_assembly_pass(argc,argv)25947427Sdonn perform_an_assembly_pass (argc, argv)
26047427Sdonn int	argc;
26147427Sdonn char **	argv;
26247427Sdonn {
26347427Sdonn 	char *	buffer;		/* Where each bufferful of lines will start. */
26447427Sdonn 	void	read_a_source_file();
26547427Sdonn 	int saw_a_file = 0;
26647427Sdonn 
26747427Sdonn 	text_fix_root		= NULL;
26847427Sdonn 	data_fix_root		= NULL;
26947427Sdonn 	need_pass_2		= FALSE;
27047427Sdonn 
27147427Sdonn 	argv++;			/* skip argv[0] */
27247427Sdonn 	argc--;			/* skip argv[0] */
27347427Sdonn 	while (argc--) {
27447427Sdonn 		if (*argv) {		/* Is it a file-name argument? */
27547427Sdonn 			/* argv -> "" if stdin desired, else -> filename */
27647427Sdonn 			if (buffer = input_scrub_new_file (*argv) ) {
27747427Sdonn 				saw_a_file++;
27847427Sdonn 				read_a_source_file(buffer);
27947427Sdonn 			}
28047427Sdonn 		}
28147427Sdonn 		argv++;			/* completed that argv */
28247427Sdonn 	}
28347427Sdonn 	if(!saw_a_file)
28447427Sdonn 		if(buffer = input_scrub_new_file("") )
28547427Sdonn 			read_a_source_file(buffer);
28647427Sdonn }
28747427Sdonn 
28847427Sdonn /*
28947427Sdonn  *			stralloc()
29047427Sdonn  *
29147427Sdonn  * Allocate memory for a new copy of a string. Copy the string.
29247427Sdonn  * Return the address of the new string. Die if there is any error.
29347427Sdonn  */
29447427Sdonn 
29547427Sdonn char *
stralloc(str)29647427Sdonn stralloc (str)
29747427Sdonn char *	str;
29847427Sdonn {
29947427Sdonn 	register char *	retval;
30047427Sdonn 	register long int	len;
30147427Sdonn 
30247427Sdonn 	len = strlen (str) + 1;
30347427Sdonn 	retval = xmalloc (len);
30447427Sdonn 	(void)strcpy (retval, str);
30547427Sdonn 	return (retval);
30647427Sdonn }
30747427Sdonn 
lose()30847427Sdonn lose()
30947427Sdonn {
31047427Sdonn 	as_fatal( "%s: 2nd pass not implemented - get your code from random(3)",myname );
31147427Sdonn }
31247427Sdonn 
31347427Sdonn SIGTY
got_sig(sig)31447427Sdonn got_sig(sig)
31547427Sdonn int sig;
31647427Sdonn {
31747427Sdonn 	static here_before = 0;
31847427Sdonn 
31947427Sdonn 	as_bad("Interrupted by signal %d",sig);
32047427Sdonn 	if(here_before++)
32147427Sdonn 		exit(1);
32247427Sdonn }
32347427Sdonn 
32447427Sdonn /* end: as.c */
325