x86: generalize disabling of sub-architectures
[binutils-gdb.git] / gas / output-file.c
1 /* output-file.c - Deal with the output file
2 Copyright (C) 1987-2022 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #include "as.h"
22 #include "subsegs.h"
23 #include "output-file.h"
24
25 #ifndef TARGET_MACH
26 #define TARGET_MACH 0
27 #endif
28
29 bfd *stdoutput;
30
31 void
32 output_file_create (const char *name)
33 {
34 if (name[0] == '-' && name[1] == '\0')
35 as_fatal (_("can't open a bfd on stdout %s"), name);
36
37 else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
38 {
39 bfd_error_type err = bfd_get_error ();
40
41 if (err == bfd_error_invalid_target)
42 as_fatal (_("selected target format '%s' unknown"), TARGET_FORMAT);
43 else
44 as_fatal (_("can't create %s: %s"), name, bfd_errmsg (err));
45 }
46
47 bfd_set_format (stdoutput, bfd_object);
48 bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH);
49 if (flag_traditional_format)
50 stdoutput->flags |= BFD_TRADITIONAL_FORMAT;
51 }
52
53 static void
54 stash_frchain_obs (asection *sec)
55 {
56 segment_info_type *info = seg_info (sec);
57 if (info)
58 {
59 struct frchain *frchp;
60 for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
61 obstack_ptr_grow (&notes, &frchp->frch_obstack);
62 info->frchainP = NULL;
63 }
64 }
65
66 void
67 output_file_close (const char *filename)
68 {
69 bool res;
70 bfd *obfd = stdoutput;
71 struct obstack **obs;
72 asection *sec;
73
74 if (obfd == NULL)
75 return;
76
77 /* Prevent an infinite loop - if the close failed we will call as_fatal
78 which will call xexit() which may call this function again... */
79 stdoutput = NULL;
80
81 /* We can't free obstacks attached to the output bfd sections before
82 closing the output bfd since data in those obstacks may need to
83 be accessed, but we can't access anything in the output bfd after
84 it is closed.. */
85 for (sec = obfd->sections; sec; sec = sec->next)
86 stash_frchain_obs (sec);
87 stash_frchain_obs (reg_section);
88 stash_frchain_obs (expr_section);
89 stash_frchain_obs (bfd_abs_section_ptr);
90 stash_frchain_obs (bfd_und_section_ptr);
91 obstack_ptr_grow (&notes, NULL);
92 obs = obstack_finish (&notes);
93
94 /* Close the bfd. */
95 if (!flag_always_generate_output && had_errors ())
96 res = bfd_cache_close_all ();
97 else
98 res = bfd_close (obfd);
99
100 subsegs_end (obs);
101
102 if (! res)
103 as_fatal ("%s: %s", filename, bfd_errmsg (bfd_get_error ()));
104 }