gold patch committed: Preallocate disk space for the output file

Ian Lance Taylor
Tue Mar 17 22:33:00 GMT 2009

Up until now gold would open the output file, use lseek and write to set
it to the right size, use mmap to allocate it in the address space,
write to the address space, close the output file, and exit.
Unfortunately, this can mean that gold will exit while the output file
has dirty buffers in the kernel's buffer cache--buffers which are not
only dirty, but have not yet been assigned disk blocks.  If the disk is
out of space, this can mean that flushing the buffers back to disk will
fail.  Since gold has already exited, this failure will not be reported.

I committed this patch to avoid this theoretical problem by using
posix_fallocate instead of lseek/write (saving one system call).  If
posix_fallocate is not available, gold will call ftruncate; I have
included an implementation of ftruncate, from gnulib, for older systems.
In this case--when posix_fallocate is not available--gold will not
attempt to allocate disk space, so older systems remain vulnerable to
the out-of-disk-space issue.

This may unfortunately cause some loss of performance on older GNU/Linux
systems.  If the fallocate system call is not supported by the kernel or
the file system, glibc will fall back to writing a single byte to each
block.  This will work but may be slow in some cases.  Fortunately newer
versions of the kernel should support the fallocate system call.


2009-03-17  Ian Lance Taylor  <>

	* Check for chsize and posix_fallocate.  Replace
	* ftruncate.c: New file, from gnulib.
	* (posix_fallocate): Define dummy version if not
	(Output_file::map): Call posix_fallocate rather than lseek and
	* gold.h (ftruncate): Declare if not HAVE_FTRUNCATE.
	* configure,, Rebuild.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.patch
Type: text/x-patch
Size: 5284 bytes
Desc: Allocate disk space for output file
URL: <>

More information about the Binutils mailing list