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