outelf: add panic() for unknown output types
[nasm.git] / nasmlib / file.c
blob8c35f0e16762b0eedb4e5c4ab791f80c3ef47b55
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 #include "compiler.h"
35 #include "nasmlib.h"
37 #include <errno.h>
39 #ifdef HAVE_IO_H
40 # include <io.h>
41 #endif
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
46 /* Missing fseeko/ftello */
47 #ifndef HAVE_FSEEKO
48 # undef off_t /* Just in case it is a macro */
49 # ifdef HAVE__FSEEKI64
50 # define fseeko _fseeki64
51 # define ftello _ftelli64
52 # define off_t int64_t
53 # else
54 # define fseeko fseek
55 # define ftello ftell
56 # define off_t long
57 # endif
58 #endif
60 /* Can we adjust the file size without actually writing all the bytes? */
61 #ifdef HAVE_FILENO /* Useless without fileno() */
62 # ifdef HAVE__CHSIZE_S
63 # define nasm_ftruncate(fd,size) _chsize_s(fd,size)
64 # elif defined(HAVE__CHSIZE)
65 # define nasm_ftruncate(fd,size) _chsize(fd,size)
66 # elif defined(HAVE_FTRUNCATE)
67 # define nasm_ftruncate(fd,size) ftruncate(fd,size)
68 # endif
69 #endif
71 void nasm_write(const void *ptr, size_t size, FILE *f)
73 size_t n = fwrite(ptr, 1, size, f);
74 if (n != size || ferror(f) || feof(f))
75 nasm_fatal(0, "unable to write output: %s", strerror(errno));
78 #ifdef WORDS_LITTLEENDIAN
80 void fwriteint16_t(uint16_t data, FILE * fp)
82 nasm_write(&data, 2, fp);
85 void fwriteint32_t(uint32_t data, FILE * fp)
87 nasm_write(&data, 4, fp);
90 void fwriteint64_t(uint64_t data, FILE * fp)
92 nasm_write(&data, 8, fp);
95 void fwriteaddr(uint64_t data, int size, FILE * fp)
97 nasm_write(&data, size, fp);
100 #else /* not WORDS_LITTLEENDIAN */
102 void fwriteint16_t(uint16_t data, FILE * fp)
104 char buffer[2], *p = buffer;
105 WRITESHORT(p, data);
106 nasm_write(buffer, 2, fp);
109 void fwriteint32_t(uint32_t data, FILE * fp)
111 char buffer[4], *p = buffer;
112 WRITELONG(p, data);
113 nasm_write(buffer, 4, fp);
116 void fwriteint64_t(uint64_t data, FILE * fp)
118 char buffer[8], *p = buffer;
119 WRITEDLONG(p, data);
120 nasm_write(buffer, 8, fp);
123 void fwriteaddr(uint64_t data, int size, FILE * fp)
125 char buffer[8], *p = buffer;
126 WRITEADDR(p, data, size);
127 nasm_write(buffer, size, fp);
130 #endif
133 void fwritezero(size_t bytes, FILE *fp)
135 size_t blksize;
137 #ifdef nasm_ftruncate
138 if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
139 off_t pos = ftello(fp);
140 if (pos >= 0) {
141 if (!fflush(fp) &&
142 !nasm_ftruncate(fileno(fp), pos + bytes) &&
143 !fseeko(fp, pos+bytes, SEEK_SET))
144 return;
147 #endif
149 while (bytes) {
150 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
152 nasm_write(zero_buffer, blksize, fp);
153 bytes -= blksize;
157 #ifdef __GLIBC__
158 /* If we are using glibc, attempt to mmap the files for speed */
159 # define READ_TEXT_FILE "rtm"
160 # define READ_BIN_FILE "rbm"
161 #else
162 # define READ_TEXT_FILE "rt"
163 # define READ_BIN_FILE "rb"
164 #endif
165 #define WRITE_TEXT_FILE "wt"
166 #define WRITE_BIN_FILE "wb"
168 FILE *nasm_open_read(const char *filename, enum file_flags flags)
170 FILE *f;
172 f = fopen(filename, (flags & NF_TEXT) ? READ_TEXT_FILE : READ_BIN_FILE);
173 if (!f && (flags & NF_FATAL))
174 nasm_fatal(ERR_NOFILE, "unable to open input file: `%s': %s",
175 filename, strerror(errno));
177 return f;
180 FILE *nasm_open_write(const char *filename, enum file_flags flags)
182 FILE *f;
184 f = fopen(filename, (flags & NF_TEXT) ? WRITE_TEXT_FILE : WRITE_BIN_FILE);
185 if (!f && (flags & NF_FATAL))
186 nasm_fatal(ERR_NOFILE, "unable to open output file: `%s': %s",
187 filename, strerror(errno));
189 return f;