]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* output-file.c - Deal with the output file |
fd67aa11 | 2 | Copyright (C) 1987-2024 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" |
b1822093 AM |
23 | #include "sb.h" |
24 | #include "macro.h" | |
252b5132 RH |
25 | #include "output-file.h" |
26 | ||
252b5132 RH |
27 | #ifndef TARGET_MACH |
28 | #define TARGET_MACH 0 | |
29 | #endif | |
252b5132 | 30 | |
252b5132 RH |
31 | bfd *stdoutput; |
32 | ||
33 | void | |
3b4dbbbf | 34 | output_file_create (const char *name) |
252b5132 RH |
35 | { |
36 | if (name[0] == '-' && name[1] == '\0') | |
0e389e77 | 37 | as_fatal (_("can't open a bfd on stdout %s"), name); |
f740e790 | 38 | |
252b5132 RH |
39 | else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT))) |
40 | { | |
885afe7b AM |
41 | bfd_error_type err = bfd_get_error (); |
42 | ||
43 | if (err == bfd_error_invalid_target) | |
44 | as_fatal (_("selected target format '%s' unknown"), TARGET_FORMAT); | |
e7bd9ea0 | 45 | else |
885afe7b | 46 | as_fatal (_("can't create %s: %s"), name, bfd_errmsg (err)); |
252b5132 | 47 | } |
f740e790 | 48 | |
252b5132 | 49 | bfd_set_format (stdoutput, bfd_object); |
252b5132 | 50 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH); |
252b5132 RH |
51 | if (flag_traditional_format) |
52 | stdoutput->flags |= BFD_TRADITIONAL_FORMAT; | |
53 | } | |
54 | ||
eeeaf705 AM |
55 | static void |
56 | stash_frchain_obs (asection *sec) | |
57 | { | |
58 | segment_info_type *info = seg_info (sec); | |
59 | if (info) | |
60 | { | |
61 | struct frchain *frchp; | |
62 | for (frchp = info->frchainP; frchp; frchp = frchp->frch_next) | |
63 | obstack_ptr_grow (¬es, &frchp->frch_obstack); | |
64 | info->frchainP = NULL; | |
65 | } | |
66 | } | |
67 | ||
252b5132 | 68 | void |
07e64e0b | 69 | output_file_close (void) |
252b5132 | 70 | { |
5b7c81bd | 71 | bool res; |
eeeaf705 AM |
72 | bfd *obfd = stdoutput; |
73 | struct obstack **obs; | |
74 | asection *sec; | |
07e64e0b | 75 | const char *filename; |
df3ca5a3 | 76 | |
eeeaf705 | 77 | if (obfd == NULL) |
df3ca5a3 | 78 | return; |
34bca508 | 79 | |
eeeaf705 AM |
80 | /* Prevent an infinite loop - if the close failed we will call as_fatal |
81 | which will call xexit() which may call this function again... */ | |
82 | stdoutput = NULL; | |
83 | ||
84 | /* We can't free obstacks attached to the output bfd sections before | |
85 | closing the output bfd since data in those obstacks may need to | |
86 | be accessed, but we can't access anything in the output bfd after | |
87 | it is closed.. */ | |
88 | for (sec = obfd->sections; sec; sec = sec->next) | |
89 | stash_frchain_obs (sec); | |
90 | stash_frchain_obs (reg_section); | |
91 | stash_frchain_obs (expr_section); | |
92 | stash_frchain_obs (bfd_abs_section_ptr); | |
93 | stash_frchain_obs (bfd_und_section_ptr); | |
94 | obstack_ptr_grow (¬es, NULL); | |
95 | obs = obstack_finish (¬es); | |
96 | ||
252b5132 | 97 | /* Close the bfd. */ |
82194874 | 98 | if (!flag_always_generate_output && had_errors ()) |
3e7cde4d | 99 | res = bfd_close_all_done (obfd); |
2ae08483 | 100 | else |
eeeaf705 | 101 | res = bfd_close (obfd); |
da1f81c1 AM |
102 | now_seg = NULL; |
103 | now_subseg = 0; | |
df3ca5a3 | 104 | |
07e64e0b AM |
105 | filename = out_file_name; |
106 | out_file_name = NULL; | |
107 | if (!keep_it && filename) | |
108 | unlink_if_ordinary (filename); | |
109 | ||
ed2917de AM |
110 | #ifdef md_end |
111 | md_end (); | |
2ccd2276 AM |
112 | #endif |
113 | #ifdef obj_end | |
114 | obj_end (); | |
ed2917de | 115 | #endif |
b1822093 | 116 | macro_end (); |
6eb099ae | 117 | expr_end (); |
b1822093 AM |
118 | read_end (); |
119 | symbol_end (); | |
eeeaf705 | 120 | subsegs_end (obs); |
df3ca5a3 | 121 | |
07e64e0b | 122 | if (!res) |
e54ae97f | 123 | as_fatal ("%s: %s", filename, bfd_errmsg (bfd_get_error ())); |
252b5132 | 124 | } |