Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* output-file.c - Deal with the output file |
5a1964ec | 2 | Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1996, 1998, 1999, 2001, 2003 |
252b5132 RH |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to | |
f740e790 NC |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
20 | 02111-1307, USA. */ | |
252b5132 RH |
21 | |
22 | #include <stdio.h> | |
23 | ||
24 | #include "as.h" | |
25 | ||
26 | #include "output-file.h" | |
27 | ||
28 | #ifdef BFD_HEADERS | |
29 | #define USE_BFD | |
30 | #endif | |
31 | ||
32 | #ifdef BFD_ASSEMBLER | |
33 | #define USE_BFD | |
34 | #ifndef TARGET_MACH | |
35 | #define TARGET_MACH 0 | |
36 | #endif | |
37 | #endif | |
38 | ||
39 | #ifdef USE_BFD | |
40 | #include "bfd.h" | |
41 | bfd *stdoutput; | |
42 | ||
43 | void | |
24361518 | 44 | output_file_create (char *name) |
252b5132 RH |
45 | { |
46 | if (name[0] == '-' && name[1] == '\0') | |
0e389e77 | 47 | as_fatal (_("can't open a bfd on stdout %s"), name); |
f740e790 | 48 | |
252b5132 RH |
49 | else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT))) |
50 | { | |
e7bd9ea0 NC |
51 | if (bfd_get_error () == bfd_error_invalid_target) |
52 | as_perror (_("Selected target format '%s' unknown"), TARGET_FORMAT); | |
53 | else | |
54 | as_perror (_("FATAL: can't create %s"), name); | |
252b5132 RH |
55 | exit (EXIT_FAILURE); |
56 | } | |
f740e790 | 57 | |
252b5132 RH |
58 | bfd_set_format (stdoutput, bfd_object); |
59 | #ifdef BFD_ASSEMBLER | |
60 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH); | |
61 | #endif | |
62 | if (flag_traditional_format) | |
63 | stdoutput->flags |= BFD_TRADITIONAL_FORMAT; | |
64 | } | |
65 | ||
66 | void | |
24361518 | 67 | output_file_close (char *filename) |
252b5132 RH |
68 | { |
69 | #ifdef BFD_ASSEMBLER | |
70 | /* Close the bfd. */ | |
71 | if (bfd_close (stdoutput) == 0) | |
72 | { | |
73 | bfd_perror (filename); | |
0e389e77 | 74 | as_perror (_("FATAL: can't close %s\n"), filename); |
252b5132 RH |
75 | exit (EXIT_FAILURE); |
76 | } | |
77 | #else | |
f740e790 | 78 | /* Close the bfd without getting bfd to write out anything by itself. */ |
252b5132 RH |
79 | if (bfd_close_all_done (stdoutput) == 0) |
80 | { | |
0e389e77 | 81 | as_perror (_("FATAL: can't close %s\n"), filename); |
252b5132 RH |
82 | exit (EXIT_FAILURE); |
83 | } | |
84 | #endif | |
f740e790 | 85 | stdoutput = NULL; /* Trust nobody! */ |
252b5132 RH |
86 | } |
87 | ||
88 | #ifndef BFD_ASSEMBLER | |
89 | void | |
5a1964ec NC |
90 | output_file_append (char *where ATTRIBUTE_UNUSED, |
91 | long length ATTRIBUTE_UNUSED, | |
92 | char *filename ATTRIBUTE_UNUSED) | |
252b5132 RH |
93 | { |
94 | abort (); | |
95 | } | |
96 | #endif | |
97 | ||
98 | #else | |
99 | ||
100 | static FILE *stdoutput; | |
101 | ||
102 | void | |
5a1964ec | 103 | output_file_create (char *name) |
252b5132 RH |
104 | { |
105 | if (name[0] == '-' && name[1] == '\0') | |
106 | { | |
107 | stdoutput = stdout; | |
108 | return; | |
109 | } | |
110 | ||
f740e790 | 111 | stdoutput = fopen (name, FOPEN_WB); |
252b5132 RH |
112 | if (stdoutput == NULL) |
113 | { | |
5a1964ec NC |
114 | #ifdef BFD_ASSEMBLER |
115 | bfd_set_error (bfd_error_system_call); | |
116 | #endif | |
0e389e77 | 117 | as_perror (_("FATAL: can't create %s"), name); |
252b5132 RH |
118 | exit (EXIT_FAILURE); |
119 | } | |
120 | } | |
121 | ||
122 | void | |
5a1964ec | 123 | output_file_close (char *filename) |
252b5132 RH |
124 | { |
125 | if (EOF == fclose (stdoutput)) | |
126 | { | |
5a1964ec NC |
127 | #ifdef BFD_ASSEMBLER |
128 | bfd_set_error (bfd_error_system_call); | |
129 | #endif | |
0e389e77 | 130 | as_perror (_("FATAL: can't close %s"), filename); |
252b5132 RH |
131 | exit (EXIT_FAILURE); |
132 | } | |
f740e790 NC |
133 | |
134 | /* Trust nobody! */ | |
135 | stdoutput = NULL; | |
252b5132 RH |
136 | } |
137 | ||
138 | void | |
5a1964ec | 139 | output_file_append (char * where, long length, char * filename) |
252b5132 RH |
140 | { |
141 | for (; length; length--, where++) | |
142 | { | |
143 | (void) putc (*where, stdoutput); | |
f740e790 | 144 | |
252b5132 | 145 | if (ferror (stdoutput)) |
252b5132 | 146 | { |
5a1964ec NC |
147 | #ifdef BFD_ASSEMBLER |
148 | bfd_set_error (bfd_error_system_call); | |
149 | #endif | |
252b5132 | 150 | as_perror (_("Failed to emit an object byte"), filename); |
0e389e77 | 151 | as_fatal (_("can't continue")); |
252b5132 RH |
152 | } |
153 | } | |
154 | } | |
155 | ||
156 | #endif | |
157 |