Unify all-zero buffers; add fwritezero()
authorH. Peter Anvin <hpa@zytor.com>
Mon, 9 Feb 2009 10:03:33 +0000 (11:03 +0100)
committerH. Peter Anvin <hpa@zytor.com>
Sun, 22 Feb 2009 01:24:08 +0000 (17:24 -0800)
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
nasmlib.c
nasmlib.h
output/outelf32.c
output/outelf64.c
output/outmacho.c
rdoff/ldrdf.c

index ee189f7..2668857 100644 (file)
 #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;
index 91eeb2f..34dbbcf 100644 (file)
--- 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)
 {
index d001c4c..cb802b1 100644 (file)
--- 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
index 3766cdd..024b209 100644 (file)
@@ -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 */
index a12559e..9960a39 100644 (file)
@@ -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 */
index 30234a9..5387e99 100644 (file)
@@ -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)
index 046139b..d137f16 100644 (file)
@@ -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);
 }
 
 /* =========================================================================