]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* output-file.c - Deal with the output file |
f740e790 | 2 | Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1996, 1998, 1999, 2001 |
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 | { | |
0e389e77 | 51 | as_perror (_("FATAL: can't create %s"), name); |
252b5132 RH |
52 | exit (EXIT_FAILURE); |
53 | } | |
f740e790 | 54 | |
252b5132 RH |
55 | bfd_set_format (stdoutput, bfd_object); |
56 | #ifdef BFD_ASSEMBLER | |
57 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH); | |
58 | #endif | |
59 | if (flag_traditional_format) | |
60 | stdoutput->flags |= BFD_TRADITIONAL_FORMAT; | |
61 | } | |
62 | ||
63 | void | |
24361518 | 64 | output_file_close (char *filename) |
252b5132 RH |
65 | { |
66 | #ifdef BFD_ASSEMBLER | |
67 | /* Close the bfd. */ | |
68 | if (bfd_close (stdoutput) == 0) | |
69 | { | |
70 | bfd_perror (filename); | |
0e389e77 | 71 | as_perror (_("FATAL: can't close %s\n"), filename); |
252b5132 RH |
72 | exit (EXIT_FAILURE); |
73 | } | |
74 | #else | |
f740e790 | 75 | /* Close the bfd without getting bfd to write out anything by itself. */ |
252b5132 RH |
76 | if (bfd_close_all_done (stdoutput) == 0) |
77 | { | |
0e389e77 | 78 | as_perror (_("FATAL: can't close %s\n"), filename); |
252b5132 RH |
79 | exit (EXIT_FAILURE); |
80 | } | |
81 | #endif | |
f740e790 | 82 | stdoutput = NULL; /* Trust nobody! */ |
252b5132 RH |
83 | } |
84 | ||
85 | #ifndef BFD_ASSEMBLER | |
86 | void | |
87 | output_file_append (where, length, filename) | |
a04b544b ILT |
88 | char *where ATTRIBUTE_UNUSED; |
89 | long length ATTRIBUTE_UNUSED; | |
90 | char *filename ATTRIBUTE_UNUSED; | |
252b5132 RH |
91 | { |
92 | abort (); | |
93 | } | |
94 | #endif | |
95 | ||
96 | #else | |
97 | ||
98 | static FILE *stdoutput; | |
99 | ||
100 | void | |
101 | output_file_create (name) | |
102 | char *name; | |
103 | { | |
104 | if (name[0] == '-' && name[1] == '\0') | |
105 | { | |
106 | stdoutput = stdout; | |
107 | return; | |
108 | } | |
109 | ||
f740e790 | 110 | stdoutput = fopen (name, FOPEN_WB); |
252b5132 RH |
111 | if (stdoutput == NULL) |
112 | { | |
0e389e77 | 113 | as_perror (_("FATAL: can't create %s"), name); |
252b5132 RH |
114 | exit (EXIT_FAILURE); |
115 | } | |
116 | } | |
117 | ||
118 | void | |
119 | output_file_close (filename) | |
120 | char *filename; | |
121 | { | |
122 | if (EOF == fclose (stdoutput)) | |
123 | { | |
0e389e77 | 124 | as_perror (_("FATAL: can't close %s"), filename); |
252b5132 RH |
125 | exit (EXIT_FAILURE); |
126 | } | |
f740e790 NC |
127 | |
128 | /* Trust nobody! */ | |
129 | stdoutput = NULL; | |
252b5132 RH |
130 | } |
131 | ||
132 | void | |
133 | output_file_append (where, length, filename) | |
f740e790 NC |
134 | char * where; |
135 | long length; | |
136 | char * filename; | |
252b5132 RH |
137 | { |
138 | for (; length; length--, where++) | |
139 | { | |
140 | (void) putc (*where, stdoutput); | |
f740e790 | 141 | |
252b5132 RH |
142 | if (ferror (stdoutput)) |
143 | /* if ( EOF == (putc( *where, stdoutput )) ) */ | |
144 | { | |
145 | as_perror (_("Failed to emit an object byte"), filename); | |
0e389e77 | 146 | as_fatal (_("can't continue")); |
252b5132 RH |
147 | } |
148 | } | |
149 | } | |
150 | ||
151 | #endif | |
152 |