output-file.c revision 130561
133965Sjdp/* output-file.c -  Deal with the output file
2130561Sobrien   Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1996, 1998, 1999, 2001, 2003
333965Sjdp   Free Software Foundation, Inc.
433965Sjdp
533965Sjdp   This file is part of GAS, the GNU Assembler.
633965Sjdp
733965Sjdp   GAS is free software; you can redistribute it and/or modify
833965Sjdp   it under the terms of the GNU General Public License as published by
933965Sjdp   the Free Software Foundation; either version 2, or (at your option)
1033965Sjdp   any later version.
1133965Sjdp
1233965Sjdp   GAS is distributed in the hope that it will be useful,
1333965Sjdp   but WITHOUT ANY WARRANTY; without even the implied warranty of
1433965Sjdp   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1533965Sjdp   GNU General Public License for more details.
1633965Sjdp
1733965Sjdp   You should have received a copy of the GNU General Public License
1833965Sjdp   along with GAS; see the file COPYING.  If not, write to
1989857Sobrien   the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2089857Sobrien   02111-1307, USA.  */
2133965Sjdp
2233965Sjdp#include <stdio.h>
2333965Sjdp
2433965Sjdp#include "as.h"
2533965Sjdp
2633965Sjdp#include "output-file.h"
2733965Sjdp
2833965Sjdp#ifdef BFD_HEADERS
2933965Sjdp#define USE_BFD
3033965Sjdp#endif
3133965Sjdp
3233965Sjdp#ifdef BFD_ASSEMBLER
3333965Sjdp#define USE_BFD
3433965Sjdp#ifndef TARGET_MACH
3533965Sjdp#define TARGET_MACH 0
3633965Sjdp#endif
3733965Sjdp#endif
3833965Sjdp
3933965Sjdp#ifdef USE_BFD
4033965Sjdp#include "bfd.h"
4133965Sjdpbfd *stdoutput;
4233965Sjdp
4333965Sjdpvoid
44130561Sobrienoutput_file_create (char *name)
4533965Sjdp{
4633965Sjdp  if (name[0] == '-' && name[1] == '\0')
4789857Sobrien    as_fatal (_("can't open a bfd on stdout %s"), name);
4889857Sobrien
4933965Sjdp  else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
5033965Sjdp    {
5189857Sobrien      as_perror (_("FATAL: can't create %s"), name);
5233965Sjdp      exit (EXIT_FAILURE);
5333965Sjdp    }
5489857Sobrien
5533965Sjdp  bfd_set_format (stdoutput, bfd_object);
5633965Sjdp#ifdef BFD_ASSEMBLER
5733965Sjdp  bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH);
5833965Sjdp#endif
5938889Sjdp  if (flag_traditional_format)
6038889Sjdp    stdoutput->flags |= BFD_TRADITIONAL_FORMAT;
6133965Sjdp}
6233965Sjdp
6333965Sjdpvoid
64130561Sobrienoutput_file_close (char *filename)
6533965Sjdp{
6633965Sjdp#ifdef BFD_ASSEMBLER
6733965Sjdp  /* Close the bfd.  */
6833965Sjdp  if (bfd_close (stdoutput) == 0)
6933965Sjdp    {
7033965Sjdp      bfd_perror (filename);
7189857Sobrien      as_perror (_("FATAL: can't close %s\n"), filename);
7233965Sjdp      exit (EXIT_FAILURE);
7333965Sjdp    }
7433965Sjdp#else
7589857Sobrien  /* Close the bfd without getting bfd to write out anything by itself.  */
7633965Sjdp  if (bfd_close_all_done (stdoutput) == 0)
7733965Sjdp    {
7889857Sobrien      as_perror (_("FATAL: can't close %s\n"), filename);
7933965Sjdp      exit (EXIT_FAILURE);
8033965Sjdp    }
8133965Sjdp#endif
8289857Sobrien  stdoutput = NULL;		/* Trust nobody!  */
8333965Sjdp}
8433965Sjdp
8533965Sjdp#ifndef BFD_ASSEMBLER
8633965Sjdpvoid
87130561Sobrienoutput_file_append (char *where ATTRIBUTE_UNUSED,
88130561Sobrien		    long length ATTRIBUTE_UNUSED,
89130561Sobrien		    char *filename ATTRIBUTE_UNUSED)
9033965Sjdp{
9133965Sjdp  abort ();
9233965Sjdp}
9333965Sjdp#endif
9433965Sjdp
9533965Sjdp#else
9633965Sjdp
9733965Sjdpstatic FILE *stdoutput;
9833965Sjdp
9933965Sjdpvoid
100130561Sobrienoutput_file_create (char *name)
10133965Sjdp{
10233965Sjdp  if (name[0] == '-' && name[1] == '\0')
10333965Sjdp    {
10433965Sjdp      stdoutput = stdout;
10533965Sjdp      return;
10633965Sjdp    }
10733965Sjdp
10889857Sobrien  stdoutput = fopen (name, FOPEN_WB);
10933965Sjdp  if (stdoutput == NULL)
11033965Sjdp    {
111130561Sobrien#ifdef BFD_ASSEMBLER
112130561Sobrien      bfd_set_error (bfd_error_system_call);
113130561Sobrien#endif
11489857Sobrien      as_perror (_("FATAL: can't create %s"), name);
11533965Sjdp      exit (EXIT_FAILURE);
11633965Sjdp    }
11733965Sjdp}
11833965Sjdp
11933965Sjdpvoid
120130561Sobrienoutput_file_close (char *filename)
12133965Sjdp{
12233965Sjdp  if (EOF == fclose (stdoutput))
12333965Sjdp    {
124130561Sobrien#ifdef BFD_ASSEMBLER
125130561Sobrien      bfd_set_error (bfd_error_system_call);
126130561Sobrien#endif
12789857Sobrien      as_perror (_("FATAL: can't close %s"), filename);
12833965Sjdp      exit (EXIT_FAILURE);
12933965Sjdp    }
13089857Sobrien
13189857Sobrien  /* Trust nobody!  */
13289857Sobrien  stdoutput = NULL;
13333965Sjdp}
13433965Sjdp
13533965Sjdpvoid
136130561Sobrienoutput_file_append (char * where, long length, char * filename)
13733965Sjdp{
13833965Sjdp  for (; length; length--, where++)
13933965Sjdp    {
14033965Sjdp      (void) putc (*where, stdoutput);
14189857Sobrien
14233965Sjdp      if (ferror (stdoutput))
14333965Sjdp	{
144130561Sobrien#ifdef BFD_ASSEMBLER
145130561Sobrien	  bfd_set_error (bfd_error_system_call);
146130561Sobrien#endif
14760484Sobrien	  as_perror (_("Failed to emit an object byte"), filename);
14889857Sobrien	  as_fatal (_("can't continue"));
14933965Sjdp	}
15033965Sjdp    }
15133965Sjdp}
15233965Sjdp
15333965Sjdp#endif
15433965Sjdp
155