xref: /dflybsd-src/contrib/binutils-2.34/gas/output-file.c (revision b52ef7118d1621abed722c5bbbd542210290ecef)
1*fae548d3Szrj /* output-file.c -  Deal with the output file
2*fae548d3Szrj    Copyright (C) 1987-2020 Free Software Foundation, Inc.
3*fae548d3Szrj 
4*fae548d3Szrj    This file is part of GAS, the GNU Assembler.
5*fae548d3Szrj 
6*fae548d3Szrj    GAS is free software; you can redistribute it and/or modify
7*fae548d3Szrj    it under the terms of the GNU General Public License as published by
8*fae548d3Szrj    the Free Software Foundation; either version 3, or (at your option)
9*fae548d3Szrj    any later version.
10*fae548d3Szrj 
11*fae548d3Szrj    GAS is distributed in the hope that it will be useful,
12*fae548d3Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*fae548d3Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*fae548d3Szrj    GNU General Public License for more details.
15*fae548d3Szrj 
16*fae548d3Szrj    You should have received a copy of the GNU General Public License
17*fae548d3Szrj    along with GAS; see the file COPYING.  If not, write to
18*fae548d3Szrj    the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19*fae548d3Szrj    02110-1301, USA.  */
20*fae548d3Szrj 
21*fae548d3Szrj #include "as.h"
22*fae548d3Szrj #include "output-file.h"
23*fae548d3Szrj 
24*fae548d3Szrj #ifndef TARGET_MACH
25*fae548d3Szrj #define TARGET_MACH 0
26*fae548d3Szrj #endif
27*fae548d3Szrj 
28*fae548d3Szrj bfd *stdoutput;
29*fae548d3Szrj 
30*fae548d3Szrj void
output_file_create(const char * name)31*fae548d3Szrj output_file_create (const char *name)
32*fae548d3Szrj {
33*fae548d3Szrj   if (name[0] == '-' && name[1] == '\0')
34*fae548d3Szrj     as_fatal (_("can't open a bfd on stdout %s"), name);
35*fae548d3Szrj 
36*fae548d3Szrj   else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
37*fae548d3Szrj     {
38*fae548d3Szrj       bfd_error_type err = bfd_get_error ();
39*fae548d3Szrj 
40*fae548d3Szrj       if (err == bfd_error_invalid_target)
41*fae548d3Szrj 	as_fatal (_("selected target format '%s' unknown"), TARGET_FORMAT);
42*fae548d3Szrj       else
43*fae548d3Szrj 	as_fatal (_("can't create %s: %s"), name, bfd_errmsg (err));
44*fae548d3Szrj     }
45*fae548d3Szrj 
46*fae548d3Szrj   bfd_set_format (stdoutput, bfd_object);
47*fae548d3Szrj   bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH);
48*fae548d3Szrj   if (flag_traditional_format)
49*fae548d3Szrj     stdoutput->flags |= BFD_TRADITIONAL_FORMAT;
50*fae548d3Szrj }
51*fae548d3Szrj 
52*fae548d3Szrj void
output_file_close(const char * filename)53*fae548d3Szrj output_file_close (const char *filename)
54*fae548d3Szrj {
55*fae548d3Szrj   bfd_boolean res;
56*fae548d3Szrj 
57*fae548d3Szrj   if (stdoutput == NULL)
58*fae548d3Szrj     return;
59*fae548d3Szrj 
60*fae548d3Szrj   /* Close the bfd.  */
61*fae548d3Szrj   if (had_errors ())
62*fae548d3Szrj     res = bfd_cache_close_all ();
63*fae548d3Szrj   else
64*fae548d3Szrj     res = bfd_close (stdoutput);
65*fae548d3Szrj 
66*fae548d3Szrj   /* Prevent an infinite loop - if the close failed we will call as_fatal
67*fae548d3Szrj      which will call xexit() which may call this function again...  */
68*fae548d3Szrj   stdoutput = NULL;
69*fae548d3Szrj 
70*fae548d3Szrj   if (! res)
71*fae548d3Szrj     as_fatal (_("can't close %s: %s"), filename,
72*fae548d3Szrj 	      bfd_errmsg (bfd_get_error ()));
73*fae548d3Szrj }
74