]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* output-file.c - Deal with the output file |
a2c58332 | 2 | Copyright (C) 1987-2022 Free Software Foundation, Inc. |
252b5132 RH |
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 | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
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 | |
4b4da160 NC |
18 | the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "as.h" |
eeeaf705 | 22 | #include "subsegs.h" |
252b5132 RH |
23 | #include "output-file.h" |
24 | ||
252b5132 RH |
25 | #ifndef TARGET_MACH |
26 | #define TARGET_MACH 0 | |
27 | #endif | |
252b5132 | 28 | |
252b5132 RH |
29 | bfd *stdoutput; |
30 | ||
31 | void | |
3b4dbbbf | 32 | output_file_create (const char *name) |
252b5132 RH |
33 | { |
34 | if (name[0] == '-' && name[1] == '\0') | |
0e389e77 | 35 | as_fatal (_("can't open a bfd on stdout %s"), name); |
f740e790 | 36 | |
252b5132 RH |
37 | else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT))) |
38 | { | |
885afe7b AM |
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); | |
e7bd9ea0 | 43 | else |
885afe7b | 44 | as_fatal (_("can't create %s: %s"), name, bfd_errmsg (err)); |
252b5132 | 45 | } |
f740e790 | 46 | |
252b5132 | 47 | bfd_set_format (stdoutput, bfd_object); |
252b5132 | 48 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH); |
252b5132 RH |
49 | if (flag_traditional_format) |
50 | stdoutput->flags |= BFD_TRADITIONAL_FORMAT; | |
51 | } | |
52 | ||
eeeaf705 AM |
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 (¬es, &frchp->frch_obstack); | |
62 | info->frchainP = NULL; | |
63 | } | |
64 | } | |
65 | ||
252b5132 | 66 | void |
07e64e0b | 67 | output_file_close (void) |
252b5132 | 68 | { |
5b7c81bd | 69 | bool res; |
eeeaf705 AM |
70 | bfd *obfd = stdoutput; |
71 | struct obstack **obs; | |
72 | asection *sec; | |
07e64e0b | 73 | const char *filename; |
df3ca5a3 | 74 | |
eeeaf705 | 75 | if (obfd == NULL) |
df3ca5a3 | 76 | return; |
34bca508 | 77 | |
eeeaf705 AM |
78 | /* Prevent an infinite loop - if the close failed we will call as_fatal |
79 | which will call xexit() which may call this function again... */ | |
80 | stdoutput = NULL; | |
81 | ||
82 | /* We can't free obstacks attached to the output bfd sections before | |
83 | closing the output bfd since data in those obstacks may need to | |
84 | be accessed, but we can't access anything in the output bfd after | |
85 | it is closed.. */ | |
86 | for (sec = obfd->sections; sec; sec = sec->next) | |
87 | stash_frchain_obs (sec); | |
88 | stash_frchain_obs (reg_section); | |
89 | stash_frchain_obs (expr_section); | |
90 | stash_frchain_obs (bfd_abs_section_ptr); | |
91 | stash_frchain_obs (bfd_und_section_ptr); | |
92 | obstack_ptr_grow (¬es, NULL); | |
93 | obs = obstack_finish (¬es); | |
94 | ||
252b5132 | 95 | /* Close the bfd. */ |
82194874 | 96 | if (!flag_always_generate_output && had_errors ()) |
2ae08483 L |
97 | res = bfd_cache_close_all (); |
98 | else | |
eeeaf705 | 99 | res = bfd_close (obfd); |
df3ca5a3 | 100 | |
07e64e0b AM |
101 | filename = out_file_name; |
102 | out_file_name = NULL; | |
103 | if (!keep_it && filename) | |
104 | unlink_if_ordinary (filename); | |
105 | ||
eeeaf705 | 106 | subsegs_end (obs); |
df3ca5a3 | 107 | |
07e64e0b | 108 | if (!res) |
e54ae97f | 109 | as_fatal ("%s: %s", filename, bfd_errmsg (bfd_get_error ())); |
252b5132 | 110 | } |