diff --git a/nasmlib.c b/nasmlib.c index 4588ff38..b16d1f36 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -1,6 +1,6 @@ /* ----------------------------------------------------------------------- * * - * Copyright 1996-2013 The NASM Authors - All Rights Reserved + * Copyright 1996-2014 The NASM Authors - All Rights Reserved * See the file AUTHORS included with the NASM distribution for * the specific copyright holders. * @@ -41,6 +41,7 @@ #include #include #include +#include #include #include "nasm.h" @@ -143,6 +144,13 @@ no_return nasm_assert_failed(const char *file, int line, const char *msg) exit(1); } +void nasm_write(const void *ptr, size_t size, FILE *f) +{ + size_t n = fwrite(ptr, 1, size, f); + if (n != size) + nasm_error(ERR_FATAL, "unable to write output: %s", strerror(errno)); +} + #ifndef nasm_stricmp int nasm_stricmp(const char *s1, const char *s2) { @@ -385,22 +393,22 @@ int32_t seg_alloc(void) void fwriteint16_t(uint16_t data, FILE * fp) { - fwrite(&data, 1, 2, fp); + nasm_write(&data, 2, fp); } void fwriteint32_t(uint32_t data, FILE * fp) { - fwrite(&data, 1, 4, fp); + nasm_write(&data, 4, fp); } void fwriteint64_t(uint64_t data, FILE * fp) { - fwrite(&data, 1, 8, fp); + nasm_write(&data, 8, fp); } void fwriteaddr(uint64_t data, int size, FILE * fp) { - fwrite(&data, 1, size, fp); + nasm_write(&data, size, fp); } #else /* not WORDS_LITTLEENDIAN */ @@ -409,50 +417,42 @@ void fwriteint16_t(uint16_t data, FILE * fp) { char buffer[2], *p = buffer; WRITESHORT(p, data); - fwrite(buffer, 1, 2, fp); + nasm_write(buffer, 2, fp); } void fwriteint32_t(uint32_t data, FILE * fp) { char buffer[4], *p = buffer; WRITELONG(p, data); - fwrite(buffer, 1, 4, fp); + nasm_write(buffer, 1, 4, fp); } void fwriteint64_t(uint64_t data, FILE * fp) { char buffer[8], *p = buffer; WRITEDLONG(p, data); - fwrite(buffer, 1, 8, fp); + nasm_write(buffer, 8, fp); } void fwriteaddr(uint64_t data, int size, FILE * fp) { char buffer[8], *p = buffer; WRITEADDR(p, data, size); - fwrite(buffer, 1, size, fp); + nasm_write(buffer, size, fp); } #endif -size_t fwritezero(size_t bytes, FILE *fp) +void fwritezero(size_t bytes, FILE *fp) { - size_t count = 0; size_t blksize; - size_t rv; while (bytes) { blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE; - rv = fwrite(zero_buffer, 1, blksize, fp); - if (!rv) - break; - - count += rv; - bytes -= rv; + nasm_write(zero_buffer, blksize, fp); + bytes -= blksize; } - - return count; } void standard_extension(char *inname, char *outname, char *extension) diff --git a/nasmlib.h b/nasmlib.h index 8f6c8852..fb2bb084 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -142,6 +142,11 @@ void nasm_free(void *); char *nasm_strdup(const char *); char *nasm_strndup(const char *, size_t); +/* + * Wrapper around fwrite() which fatal-errors on output failure. + */ +void nasm_write(const void *, size_t, FILE *); + /* * NASM assert failure */ @@ -399,7 +404,7 @@ const char *prefix_name(int); #define ZERO_BUF_SIZE 4096 extern const uint8_t zero_buffer[ZERO_BUF_SIZE]; -size_t fwritezero(size_t bytes, FILE *fp); +void fwritezero(size_t bytes, FILE *fp); static inline bool overflow_general(int64_t value, int bytes) { diff --git a/output/outas86.c b/output/outas86.c index 7af8d785..95675ef8 100644 --- a/output/outas86.c +++ b/output/outas86.c @@ -545,7 +545,7 @@ static void as86_write_section(struct Section *sect, int index) int32_t tmplen = (length > 64 ? 64 : length); fputc(0x40 | (tmplen & 0x3F), ofile); saa_rnbytes(sect->data, buf, tmplen); - fwrite(buf, 1, tmplen, ofile); + nasm_write(buf, tmplen, ofile); length -= tmplen; } while (length > 0); break; diff --git a/output/outbin.c b/output/outbin.c index c600e557..01eae1cf 100644 --- a/output/outbin.c +++ b/output/outbin.c @@ -1468,8 +1468,8 @@ static void do_output_bin(void) } /* Generate Intel hex file output */ -static int write_ith_record(unsigned int len, uint16_t addr, - uint8_t type, void *data) +static void write_ith_record(unsigned int len, uint16_t addr, + uint8_t type, void *data) { char buf[1+2+4+2+255*2+2+2]; char *p = buf; @@ -1488,10 +1488,7 @@ static int write_ith_record(unsigned int len, uint16_t addr, p += sprintf(p, "%02X", dptr[i]); p += sprintf(p, "%02X\n", csum); - if (fwrite(buf, 1, p-buf, ofile) != (size_t)(p-buf)) - return -1; - - return 0; + nasm_write(buf, p-buf, ofile); } static void do_output_ith(void) @@ -1542,8 +1539,8 @@ static void do_output_ith(void) } /* Generate Motorola S-records */ -static int write_srecord(unsigned int len, unsigned int alen, - uint32_t addr, uint8_t type, void *data) +static void write_srecord(unsigned int len, unsigned int alen, + uint32_t addr, uint8_t type, void *data) { char buf[2+2+8+255*2+2+2]; char *p = buf; @@ -1576,10 +1573,7 @@ static int write_srecord(unsigned int len, unsigned int alen, p += sprintf(p, "%02X", dptr[i]); p += sprintf(p, "%02X\n", csum); - if (fwrite(buf, 1, p-buf, ofile) != (size_t)(p-buf)) - return -1; - - return 0; + nasm_write(buf, p-buf, ofile); } static void do_output_srec(void) diff --git a/output/outcoff.c b/output/outcoff.c index 3949028b..5af063b5 100644 --- a/output/outcoff.c +++ b/output/outcoff.c @@ -998,7 +998,7 @@ static void coff_section_header(char *name, int32_t namepos, int32_t vsize, if (namepos == -1) { strncpy(padname, name, 8); - fwrite(padname, 8, 1, ofile); + nasm_write(padname, 8, ofile); } else { /* * If name is longer than 8 bytes, write '/' followed @@ -1020,7 +1020,7 @@ static void coff_section_header(char *name, int32_t namepos, int32_t vsize, padname[6] = '0' + (namepos / 10); namepos = namepos % 10; padname[7] = '0' + (namepos); - fwrite(padname, 8, 1, ofile); + nasm_write(padname, 8, ofile); } fwriteint32_t(0, ofile); /* Virtual size field - set to 0 or vsize */ @@ -1073,7 +1073,7 @@ static void coff_symbol(char *name, int32_t strpos, int32_t value, if (name) { strncpy(padname, name, 8); - fwrite(padname, 8, 1, ofile); + nasm_write(padname, 8, ofile); } else { fwriteint32_t(0, ofile); fwriteint32_t(strpos, ofile); @@ -1097,7 +1097,7 @@ static void coff_write_symbols(void) */ coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1); strncpy(filename, coff_infile, 18); - fwrite(filename, 18, 1, ofile); + nasm_write(filename, 18, ofile); /* * The section records, with their auxiliaries. @@ -1108,7 +1108,7 @@ static void coff_write_symbols(void) coff_symbol(sects[i]->name, 0L, 0L, i + 1, 0, 3, 1); fwriteint32_t(sects[i]->len, ofile); fwriteint16_t(sects[i]->nrelocs,ofile); - fwrite(filename, 12, 1, ofile); + nasm_write(filename, 12, ofile); } /* diff --git a/output/outelf32.c b/output/outelf32.c index af0547af..b55853ce 100644 --- a/output/outelf32.c +++ b/output/outelf32.c @@ -935,7 +935,7 @@ static void elf_write(void) /* * Output the ELF header. */ - fwrite("\177ELF\1\1\1", 7, 1, ofile); + nasm_write("\177ELF\1\1\1", 7, ofile); fputc(elf_osabi, ofile); fputc(elf_abiver, ofile); fwritezero(7, ofile); @@ -1286,7 +1286,7 @@ static void elf_write_sections(void) if (elf_sects[i].is_saa) saa_fpwrite(elf_sects[i].data, ofile); else - fwrite(elf_sects[i].data, len, 1, ofile); + nasm_write(elf_sects[i].data, len, ofile); fwritezero(align, ofile); } } diff --git a/output/outelf64.c b/output/outelf64.c index 0e6b6bc3..241e345f 100644 --- a/output/outelf64.c +++ b/output/outelf64.c @@ -1025,7 +1025,7 @@ static void elf_write(void) /* * Output the ELF header. */ - fwrite("\177ELF\2\1\1", 7, 1, ofile); + nasm_write("\177ELF\2\1\1", 7, ofile); fputc(elf_osabi, ofile); fputc(elf_abiver, ofile); fwritezero(7, ofile); @@ -1374,7 +1374,7 @@ static void elf_write_sections(void) if (elf_sects[i].is_saa) saa_fpwrite(elf_sects[i].data, ofile); else - fwrite(elf_sects[i].data, len, 1, ofile); + nasm_write(elf_sects[i].data, len, ofile); fwritezero(align, ofile); } } diff --git a/output/outelfx32.c b/output/outelfx32.c index 6b352a22..a9b6957a 100644 --- a/output/outelfx32.c +++ b/output/outelfx32.c @@ -983,7 +983,7 @@ static void elf_write(void) /* * Output the ELF header. */ - fwrite("\177ELF\1\1\1", 7, 1, ofile); + nasm_write("\177ELF\1\1\1", 7, ofile); fputc(elf_osabi, ofile); fputc(elf_abiver, ofile); fwritezero(7, ofile); @@ -1334,7 +1334,7 @@ static void elf_write_sections(void) if (elf_sects[i].is_saa) saa_fpwrite(elf_sects[i].data, ofile); else - fwrite(elf_sects[i].data, len, 1, ofile); + nasm_write(elf_sects[i].data, len, ofile); fwritezero(align, ofile); } } diff --git a/output/outmac32.c b/output/outmac32.c index c2c91b48..616211eb 100644 --- a/output/outmac32.c +++ b/output/outmac32.c @@ -883,8 +883,8 @@ static uint32_t macho_write_segment (uint32_t offset) /* emit section headers */ for (s = sects; s != NULL; s = s->next) { - fwrite(s->sectname, sizeof(s->sectname), 1, ofile); - fwrite(s->segname, sizeof(s->segname), 1, ofile); + nasm_write(s->sectname, sizeof(s->sectname), ofile); + nasm_write(s->segname, sizeof(s->segname), ofile); fwriteint32_t(s->addr, ofile); fwriteint32_t(s->size, ofile); @@ -1050,8 +1050,8 @@ static void macho_write_symtab (void) for (i = 0; i < nextdefsym; i++) { sym = extdefsyms[i]; fwriteint32_t(sym->strx, ofile); - fwrite(&sym->type, 1, 1, ofile); /* symbol type */ - fwrite(&sym->sect, 1, 1, ofile); /* section */ + nasm_write(&sym->type, 1, ofile); /* symbol type */ + nasm_write(&sym->sect, 1, ofile); /* section */ fwriteint16_t(sym->desc, ofile); /* description */ /* Fix up the symbol value now that we know the final section @@ -1068,8 +1068,8 @@ static void macho_write_symtab (void) for (i = 0; i < nundefsym; i++) { sym = undefsyms[i]; fwriteint32_t(sym->strx, ofile); - fwrite(&sym->type, 1, 1, ofile); /* symbol type */ - fwrite(&sym->sect, 1, 1, ofile); /* section */ + nasm_write(&sym->type, 1, ofile); /* symbol type */ + nasm_write(&sym->sect, 1, ofile); /* section */ fwriteint16_t(sym->desc, ofile); /* description */ /* Fix up the symbol value now that we know the final section diff --git a/output/outmac64.c b/output/outmac64.c index abad84aa..44dcf0c7 100644 --- a/output/outmac64.c +++ b/output/outmac64.c @@ -1005,7 +1005,7 @@ static void macho_write_header (void) fwriteint32_t(head_ncmds64, ofile); /* number of load commands */ fwriteint32_t(head_sizeofcmds64, ofile); /* size of load commands */ fwriteint32_t(0, ofile); /* no flags */ - fwriteint32_t(0, ofile); /* reserved for future use */ + fwriteint32_t(0, ofile); /* reserved for future use */ } /* Write out the segment load command at offset. */ @@ -1036,8 +1036,8 @@ static uint32_t macho_write_segment (uint64_t offset) /* emit section headers */ for (s = sects; s != NULL; s = s->next) { - fwrite(s->sectname, sizeof(s->sectname), 1, ofile); - fwrite(s->segname, sizeof(s->segname), 1, ofile); + nasm_write(s->sectname, sizeof(s->sectname), ofile); + nasm_write(s->segname, sizeof(s->segname), ofile); fwriteint64_t(s->addr, ofile); fwriteint64_t(s->size, ofile); @@ -1202,9 +1202,9 @@ static void macho_write_symtab (void) for (sym = syms; sym != NULL; sym = sym->next) { if ((sym->type & N_EXT) == 0) { fwriteint32_t(sym->strx, ofile); /* string table entry number */ - fwrite(&sym->type, 1, 1, ofile); /* symbol type */ - fwrite(&sym->sect, 1, 1, ofile); /* section */ - fwriteint16_t(sym->desc, ofile); /* description */ + nasm_write(&sym->type, 1, ofile); /* symbol type */ + nasm_write(&sym->sect, 1, ofile); /* section */ + fwriteint16_t(sym->desc, ofile); /* description */ /* Fix up the symbol value now that we know the final section sizes. */ @@ -1224,8 +1224,8 @@ static void macho_write_symtab (void) for (i = 0; i < nextdefsym; i++) { sym = extdefsyms[i]; fwriteint32_t(sym->strx, ofile); - fwrite(&sym->type, 1, 1, ofile); /* symbol type */ - fwrite(&sym->sect, 1, 1, ofile); /* section */ + nasm_write(&sym->type, 1, ofile); /* symbol type */ + nasm_write(&sym->sect, 1, ofile); /* section */ fwriteint16_t(sym->desc, ofile); /* description */ /* Fix up the symbol value now that we know the final section @@ -1242,8 +1242,8 @@ static void macho_write_symtab (void) for (i = 0; i < nundefsym; i++) { sym = undefsyms[i]; fwriteint32_t(sym->strx, ofile); - fwrite(&sym->type, 1, 1, ofile); /* symbol type */ - fwrite(&sym->sect, 1, 1, ofile); /* section */ + nasm_write(&sym->type, 1, ofile); /* symbol type */ + nasm_write(&sym->sect, 1, ofile); /* section */ fwriteint16_t(sym->desc, ofile); /* description */ // Fix up the symbol value now that we know the final section sizes. diff --git a/output/outobj.c b/output/outobj.c index 7fa29b27..225ea4fe 100644 --- a/output/outobj.c +++ b/output/outobj.c @@ -2396,7 +2396,7 @@ static void obj_fwrite(ObjRecord * orp) len = orp->committed + 1; cksum += (len & 0xFF) + ((len >> 8) & 0xFF); fwriteint16_t(len, ofile); - fwrite(orp->buf, 1, len - 1, ofile); + nasm_write(orp->buf, len-1, ofile); for (ptr = orp->buf; --len; ptr++) cksum += *ptr; fputc((-cksum) & 0xFF, ofile); diff --git a/output/outrdf2.c b/output/outrdf2.c index c7aeb28b..fdac5ee3 100644 --- a/output/outrdf2.c +++ b/output/outrdf2.c @@ -670,7 +670,7 @@ static void rdf2_cleanup(int debuginfo) /* should write imported & exported symbol declarations to header here */ /* generate the output file... */ - fwrite(RDOFF2Id, 6, 1, ofile); /* file type magic number */ + nasm_write(RDOFF2Id, 6, ofile); /* file type magic number */ if (bsslength != 0) { /* reserve BSS */ bs.type = RDFREC_BSS; diff --git a/saa.c b/saa.c index 6719584a..a0350c10 100644 --- a/saa.c +++ b/saa.c @@ -278,7 +278,7 @@ void saa_fpwrite(struct SAA *s, FILE * fp) saa_rewind(s); while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL) - fwrite(data, 1, len, fp); + nasm_write(data, len, fp); } void saa_write8(struct SAA *s, uint8_t v)