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