| 1 | /* output-file.c - Deal with the output file |
| 2 | Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1996, 1998, 1999 |
| 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 |
| 19 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include "as.h" |
| 24 | |
| 25 | #include "output-file.h" |
| 26 | |
| 27 | #ifdef BFD_HEADERS |
| 28 | #define USE_BFD |
| 29 | #endif |
| 30 | |
| 31 | #ifdef BFD_ASSEMBLER |
| 32 | #define USE_BFD |
| 33 | #ifndef TARGET_MACH |
| 34 | #define TARGET_MACH 0 |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | #ifdef USE_BFD |
| 39 | #include "bfd.h" |
| 40 | bfd *stdoutput; |
| 41 | |
| 42 | void |
| 43 | output_file_create (name) |
| 44 | char *name; |
| 45 | { |
| 46 | if (name[0] == '-' && name[1] == '\0') |
| 47 | { |
| 48 | as_fatal (_("Can't open a bfd on stdout %s "), name); |
| 49 | } |
| 50 | else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT))) |
| 51 | { |
| 52 | as_perror (_("FATAL: Can't create %s"), name); |
| 53 | exit (EXIT_FAILURE); |
| 54 | } |
| 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 |
| 64 | output_file_close (filename) |
| 65 | char *filename; |
| 66 | { |
| 67 | #ifdef BFD_ASSEMBLER |
| 68 | /* Close the bfd. */ |
| 69 | if (bfd_close (stdoutput) == 0) |
| 70 | { |
| 71 | bfd_perror (filename); |
| 72 | as_perror (_("FATAL: Can't close %s\n"), filename); |
| 73 | exit (EXIT_FAILURE); |
| 74 | } |
| 75 | #else |
| 76 | /* Close the bfd without getting bfd to write out anything by itself */ |
| 77 | if (bfd_close_all_done (stdoutput) == 0) |
| 78 | { |
| 79 | as_perror (_("FATAL: Can't close %s\n"), filename); |
| 80 | exit (EXIT_FAILURE); |
| 81 | } |
| 82 | #endif |
| 83 | stdoutput = NULL; /* Trust nobody! */ |
| 84 | } |
| 85 | |
| 86 | #ifndef BFD_ASSEMBLER |
| 87 | void |
| 88 | output_file_append (where, length, filename) |
| 89 | char *where ATTRIBUTE_UNUSED; |
| 90 | long length ATTRIBUTE_UNUSED; |
| 91 | char *filename ATTRIBUTE_UNUSED; |
| 92 | { |
| 93 | abort (); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | #else |
| 98 | |
| 99 | static FILE *stdoutput; |
| 100 | |
| 101 | void |
| 102 | output_file_create (name) |
| 103 | char *name; |
| 104 | { |
| 105 | if (name[0] == '-' && name[1] == '\0') |
| 106 | { |
| 107 | stdoutput = stdout; |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | stdoutput = fopen (name, "wb"); |
| 112 | |
| 113 | /* Some systems don't grok "b" in fopen modes. */ |
| 114 | if (stdoutput == NULL) |
| 115 | stdoutput = fopen (name, "w"); |
| 116 | |
| 117 | if (stdoutput == NULL) |
| 118 | { |
| 119 | as_perror (_("FATAL: Can't create %s"), name); |
| 120 | exit (EXIT_FAILURE); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | output_file_close (filename) |
| 126 | char *filename; |
| 127 | { |
| 128 | if (EOF == fclose (stdoutput)) |
| 129 | { |
| 130 | as_perror (_("FATAL: Can't close %s"), filename); |
| 131 | exit (EXIT_FAILURE); |
| 132 | } |
| 133 | stdoutput = NULL; /* Trust nobody! */ |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | output_file_append (where, length, filename) |
| 138 | char *where; |
| 139 | long length; |
| 140 | char *filename; |
| 141 | { |
| 142 | for (; length; length--, where++) |
| 143 | { |
| 144 | (void) putc (*where, stdoutput); |
| 145 | if (ferror (stdoutput)) |
| 146 | /* if ( EOF == (putc( *where, stdoutput )) ) */ |
| 147 | { |
| 148 | as_perror (_("Failed to emit an object byte"), filename); |
| 149 | as_fatal (_("Can't continue")); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #endif |
| 155 | |
| 156 | /* end of output-file.c */ |