output-file.c revision 38889
133965Sjdp/* output-file.c -  Deal with the output file
238889Sjdp   Copyright (C) 1987, 90, 91, 93, 92, 94, 95, 96, 1998
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
1933965Sjdp   the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2033965Sjdp
2133965Sjdp#include <stdio.h>
2233965Sjdp
2333965Sjdp#include "as.h"
2433965Sjdp
2533965Sjdp#include "output-file.h"
2633965Sjdp
2733965Sjdp#ifdef BFD_HEADERS
2833965Sjdp#define USE_BFD
2933965Sjdp#endif
3033965Sjdp
3133965Sjdp#ifdef BFD_ASSEMBLER
3233965Sjdp#define USE_BFD
3333965Sjdp#ifndef TARGET_MACH
3433965Sjdp#define TARGET_MACH 0
3533965Sjdp#endif
3633965Sjdp#endif
3733965Sjdp
3833965Sjdp#ifdef USE_BFD
3933965Sjdp#include "bfd.h"
4033965Sjdpbfd *stdoutput;
4133965Sjdp
4233965Sjdpvoid
4333965Sjdpoutput_file_create (name)
4433965Sjdp     char *name;
4533965Sjdp{
4633965Sjdp  if (name[0] == '-' && name[1] == '\0')
4733965Sjdp    {
4833965Sjdp      as_fatal ("Can't open a bfd on stdout %s ", name);
4933965Sjdp    }
5033965Sjdp  else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
5133965Sjdp    {
5233965Sjdp      as_perror ("FATAL: Can't create %s", name);
5333965Sjdp      exit (EXIT_FAILURE);
5433965Sjdp    }
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
6433965Sjdpoutput_file_close (filename)
6533965Sjdp     char *filename;
6633965Sjdp{
6733965Sjdp#ifdef BFD_ASSEMBLER
6833965Sjdp  /* Close the bfd.  */
6933965Sjdp  if (bfd_close (stdoutput) == 0)
7033965Sjdp    {
7133965Sjdp      bfd_perror (filename);
7233965Sjdp      as_perror ("FATAL: Can't close %s\n", filename);
7333965Sjdp      exit (EXIT_FAILURE);
7433965Sjdp    }
7533965Sjdp#else
7633965Sjdp  /* Close the bfd without getting bfd to write out anything by itself */
7733965Sjdp  if (bfd_close_all_done (stdoutput) == 0)
7833965Sjdp    {
7933965Sjdp      as_perror ("FATAL: Can't close %s\n", filename);
8033965Sjdp      exit (EXIT_FAILURE);
8133965Sjdp    }
8233965Sjdp#endif
8333965Sjdp  stdoutput = NULL;		/* Trust nobody! */
8433965Sjdp}
8533965Sjdp
8633965Sjdp#ifndef BFD_ASSEMBLER
8733965Sjdpvoid
8833965Sjdpoutput_file_append (where, length, filename)
8933965Sjdp     char *where;
9033965Sjdp     long length;
9133965Sjdp     char *filename;
9233965Sjdp{
9333965Sjdp  abort ();
9433965Sjdp}
9533965Sjdp#endif
9633965Sjdp
9733965Sjdp#else
9833965Sjdp
9933965Sjdpstatic FILE *stdoutput;
10033965Sjdp
10133965Sjdpvoid
10233965Sjdpoutput_file_create (name)
10333965Sjdp     char *name;
10433965Sjdp{
10533965Sjdp  if (name[0] == '-' && name[1] == '\0')
10633965Sjdp    {
10733965Sjdp      stdoutput = stdout;
10833965Sjdp      return;
10933965Sjdp    }
11033965Sjdp
11133965Sjdp  stdoutput = fopen (name, "wb");
11233965Sjdp
11333965Sjdp  /* Some systems don't grok "b" in fopen modes.  */
11433965Sjdp  if (stdoutput == NULL)
11533965Sjdp    stdoutput = fopen (name, "w");
11633965Sjdp
11733965Sjdp  if (stdoutput == NULL)
11833965Sjdp    {
11933965Sjdp      as_perror ("FATAL: Can't create %s", name);
12033965Sjdp      exit (EXIT_FAILURE);
12133965Sjdp    }
12233965Sjdp}
12333965Sjdp
12433965Sjdpvoid
12533965Sjdpoutput_file_close (filename)
12633965Sjdp     char *filename;
12733965Sjdp{
12833965Sjdp  if (EOF == fclose (stdoutput))
12933965Sjdp    {
13033965Sjdp      as_perror ("FATAL: Can't close %s", filename);
13133965Sjdp      exit (EXIT_FAILURE);
13233965Sjdp    }
13333965Sjdp  stdoutput = NULL;		/* Trust nobody! */
13433965Sjdp}
13533965Sjdp
13633965Sjdpvoid
13733965Sjdpoutput_file_append (where, length, filename)
13833965Sjdp     char *where;
13933965Sjdp     long length;
14033965Sjdp     char *filename;
14133965Sjdp{
14233965Sjdp  for (; length; length--, where++)
14333965Sjdp    {
14433965Sjdp      (void) putc (*where, stdoutput);
14533965Sjdp      if (ferror (stdoutput))
14633965Sjdp	/* if ( EOF == (putc( *where, stdoutput )) ) */
14733965Sjdp	{
14833965Sjdp	  as_perror ("Failed to emit an object byte", filename);
14933965Sjdp	  as_fatal ("Can't continue");
15033965Sjdp	}
15133965Sjdp    }
15233965Sjdp}
15333965Sjdp
15433965Sjdp#endif
15533965Sjdp
15633965Sjdp/* end of output-file.c */
157