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.
This commit is contained in:
H. Peter Anvin 2009-02-09 11:03:33 +01:00
parent 04616f4e85
commit 999868f06f
7 changed files with 34 additions and 10 deletions

View file

@ -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;

View file

@ -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)
{

View file

@ -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

View 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 */

View 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 */

View 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)

View 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);
}
/* =========================================================================