From 999868f06f49ec6450872f224a7cc0a43214b063 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 9 Feb 2009 11:03:33 +0100 Subject: [PATCH] Unify all-zero buffers; add fwritezero() We have a number of all-zero buffers in the code. Put a single all-zero buffer in nasmlib.c. Additionally, add fwritezero() which can be used to write an arbitrary number of all-zero bytes; this prevents the situation where the all-zero buffer is simply too small. --- assemble.c | 5 +---- nasmlib.c | 23 +++++++++++++++++++++++ nasmlib.h | 4 ++++ output/outelf32.c | 2 +- output/outelf64.c | 2 +- output/outmacho.c | 5 ++--- rdoff/ldrdf.c | 3 ++- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/assemble.c b/assemble.c index ee189f7c..2668857a 100644 --- a/assemble.c +++ b/assemble.c @@ -130,9 +130,6 @@ #include "insns.h" #include "tables.h" -/* Initialized to zero by the C standard */ -static const uint8_t const_zero_buf[256]; - typedef struct { int sib_present; /* is a SIB byte necessary? */ int bytes; /* # of bytes of offset needed */ @@ -353,7 +350,7 @@ int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp, if (align) { align = wsize - align; - out(offset, segment, const_zero_buf, + out(offset, segment, zero_buffer, OUT_RAWDATA, align, NO_SEG, NO_SEG); } offset += e->stringlen + align; diff --git a/nasmlib.c b/nasmlib.c index 91eeb2ff..34dbbcf5 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -25,6 +25,9 @@ efunc nasm_malloc_error; /* Exported for the benefit of vsnprintf.c */ static FILE *logfp; #endif +/* Uninitialized -> all zero by C spec */ +const uint8_t zero_buffer[ZERO_BUF_SIZE]; + /* * Prepare a table of tolower() results. This avoids function calls * on some platforms. @@ -454,6 +457,26 @@ void fwriteaddr(uint64_t data, int size, FILE * fp) #endif +size_t 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; + } + + return count; +} + void standard_extension(char *inname, char *outname, char *extension, efunc error) { diff --git a/nasmlib.h b/nasmlib.h index d001c4c2..cb802b13 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -329,4 +329,8 @@ extern struct dfmt *null_debug_arr[2]; 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); + #endif diff --git a/output/outelf32.c b/output/outelf32.c index 3766cdd7..024b209d 100644 --- a/output/outelf32.c +++ b/output/outelf32.c @@ -1106,7 +1106,7 @@ static void elf_write(void) fwrite("\177ELF\1\1\1", 7, 1, elffp); fputc(elf_osabi, elffp); fputc(elf_abiver, elffp); - fwrite("\0\0\0\0\0\0\0", 7, 1, elffp); + fwritezero(7, elffp); fwriteint16_t(1, elffp); /* ET_REL relocatable file */ fwriteint16_t(3, elffp); /* EM_386 processor ID */ fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */ diff --git a/output/outelf64.c b/output/outelf64.c index a12559e8..9960a394 100644 --- a/output/outelf64.c +++ b/output/outelf64.c @@ -1226,7 +1226,7 @@ static void elf_write(void) fwrite("\177ELF\2\1\1", 7, 1, elffp); fputc(elf_osabi, elffp); fputc(elf_abiver, elffp); - fwrite("\0\0\0\0\0\0\0", 7, 1, elffp); + fwritezero(7, elffp); fwriteint16_t(ET_REL, elffp); /* relocatable file */ fwriteint16_t(EM_X86_64, elffp); /* processor ID */ fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */ diff --git a/output/outmacho.c b/output/outmacho.c index 30234a96..5387e990 100644 --- a/output/outmacho.c +++ b/output/outmacho.c @@ -880,7 +880,7 @@ static uint32_t macho_write_segment (uint32_t offset) /* in an MH_OBJECT file all sections are in one unnamed (name ** all zeros) segment */ - fwrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 1, machofp); + fwritezero(16, machofp); fwriteint32_t(0, machofp); /* in-memory offset */ fwriteint32_t(seg_vmsize, machofp); /* in-memory size */ fwriteint32_t(offset, machofp); /* in-file offset to data */ @@ -956,7 +956,6 @@ static void macho_write_section (void) { struct section *s, *s2; struct reloc *r; - char *rel_paddata = "\0\0\0"; uint8_t *p, *q, blk[4]; int32_t l; @@ -1013,7 +1012,7 @@ static void macho_write_section (void) } /* pad last section up to reloc entries on int32_t boundary */ - fwrite(rel_paddata, rel_padcnt, 1, machofp); + fwritezero(rel_padcnt, machofp); /* emit relocation entries */ for (s = sects; s != NULL; s = s->next) diff --git a/rdoff/ldrdf.c b/rdoff/ldrdf.c index 046139b3..d137f163 100644 --- a/rdoff/ldrdf.c +++ b/rdoff/ldrdf.c @@ -37,6 +37,7 @@ #include "collectn.h" #include "rdlib.h" #include "segtab.h" +#include "nasmlib.h" #define LDRDF_VERSION "1.07" @@ -1119,7 +1120,7 @@ void write_output(const char *filename) fwrite(outputseg[i].data, outputseg[i].length, 1, f); } - fwrite("\0\0\0\0\0\0\0\0\0\0", 10, 1, f); + fwritezero(10, f); } /* ========================================================================= -- 2.11.4.GIT