[binutils-gdb] bfd: Grow in-memory BFD buffers geometrically
Jan Beulich
jbeulich@sourceware.org
Fri Nov 13 13:17:09 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=83078f2fb49152b5451a971a3f69ee12a16d96f5
commit 83078f2fb49152b5451a971a3f69ee12a16d96f5
Author: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
Date: Fri Nov 13 14:16:15 2026 +0100
bfd: Grow in-memory BFD buffers geometrically
memory_bwrite and memory_bseek grew the buffer in 128-byte steps, so
building a large in-memory BFD out of many small writes reallocated and
copied the whole buffer repeatedly. This made writing PDB streams
quadratic: linking a 235 MB executable with --pdb spent more than half
of its time in realloc.
Add a separate capacity field and grow the buffer by one eighth of its
capacity, with a minimum increase of 128 bytes. If the over-allocation
fails, retry with the exact required size.
While touching the line, switch a questionable EINVAL to ENOMEM in
memory_bseek().
bfd/
* libbfd-in.h (bfd_in_memory): Add capacity.
* libbfd.h: Regenerate.
* bfdio.c (memory_grow): New function.
(memory_bwrite, memory_bseek): Use it.
* coff-alpha.c (alpha_ecoff_get_elt_at_filepos): Initialize capacity.
* coff-bfd.c (import_make_member): Likewise.
* elfcode.h (NAME(_bfd_elf, bfd_from_remote_memory)): Likewise.
* opncls.c (bfd_make_writable): Likewise.
* peicode.h (pe_ILF_build_a_bfd): Likewise.
* xcofflink.c (bfd_xcoff_link_generate_rtinit): Likewise.
Diff:
---
bfd/bfdio.c | 80 +++++++++++++++++++++++++++++++-------------------------
bfd/coff-alpha.c | 1 +
bfd/elfcode.h | 1 +
bfd/libbfd-in.h | 4 ++-
bfd/libbfd.h | 4 ++-
bfd/opncls.c | 1 +
bfd/peicode.h | 1 +
bfd/xcofflink.c | 1 +
8 files changed, 56 insertions(+), 37 deletions(-)
diff --git a/bfd/bfdio.c b/bfd/bfdio.c
index ea0d1389cd1..a5de7abcb7a 100644
--- a/bfd/bfdio.c
+++ b/bfd/bfdio.c
@@ -784,32 +784,52 @@ memory_bread (bfd *abfd, void *ptr, file_ptr size)
return get;
}
+static bool
+memory_grow (struct bfd_in_memory *bim, bfd_size_type required)
+{
+ bfd_size_type increase, newsize;
+ bfd_byte *buffer;
+
+ increase = bim->capacity / 8;
+ if (increase < 128)
+ increase = 128;
+ if (increase > (bfd_size_type) -1 - bim->capacity)
+ newsize = required;
+ else
+ {
+ newsize = bim->capacity + increase;
+ if (newsize < required)
+ newsize = required;
+ }
+
+ buffer = bfd_realloc (bim->buffer, newsize);
+ if (buffer == NULL && newsize != required)
+ {
+ newsize = required;
+ buffer = bfd_realloc (bim->buffer, newsize);
+ if (buffer != NULL)
+ bfd_set_error (bfd_error_no_error);
+ }
+ if (buffer == NULL)
+ return false;
+
+ memset (buffer + bim->capacity, 0, newsize - bim->capacity);
+ bim->buffer = buffer;
+ bim->capacity = newsize;
+ return true;
+}
+
static file_ptr
memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
{
struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
+ bfd_size_type end = abfd->where + size;
- if (abfd->where + size > bim->size)
- {
- bfd_size_type newsize, oldsize;
-
- oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
- bim->size = abfd->where + size;
- /* Round up to cut down on memory fragmentation */
- newsize = (bim->size + 127) & ~(bfd_size_type) 127;
- if (newsize > oldsize)
- {
- bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
- if (bim->buffer == NULL)
- {
- bim->size = 0;
- return 0;
- }
- if (newsize > bim->size)
- memset (bim->buffer + bim->size, 0, newsize - bim->size);
- }
- }
+ if (end > bim->capacity && !memory_grow (bim, end))
+ return 0;
memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
+ if (end > bim->size)
+ bim->size = end;
return size;
}
@@ -844,23 +864,13 @@ memory_bseek (bfd *abfd, file_ptr position, int direction)
if (abfd->direction == write_direction
|| abfd->direction == both_direction)
{
- bfd_size_type newsize, oldsize;
-
- oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
- bim->size = nwhere;
- /* Round up to cut down on memory fragmentation */
- newsize = (bim->size + 127) & ~(bfd_size_type) 127;
- if (newsize > oldsize)
+ if ((bfd_size_type) nwhere > bim->capacity
+ && !memory_grow (bim, nwhere))
{
- bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
- if (bim->buffer == NULL)
- {
- errno = EINVAL;
- bim->size = 0;
- return -1;
- }
- memset (bim->buffer + oldsize, 0, newsize - oldsize);
+ errno = ENOMEM;
+ return -1;
}
+ bim->size = nwhere;
}
else
{
diff --git a/bfd/coff-alpha.c b/bfd/coff-alpha.c
index 77859269512..648a0daf8f8 100644
--- a/bfd/coff-alpha.c
+++ b/bfd/coff-alpha.c
@@ -2182,6 +2182,7 @@ alpha_ecoff_get_elt_at_filepos (bfd *archive, file_ptr filepos,
if (bim == NULL)
goto error_return;
bim->size = size;
+ bim->capacity = size;
bim->buffer = buf;
nbfd->mtime_set = true;
diff --git a/bfd/elfcode.h b/bfd/elfcode.h
index 5eae7f92d14..8d919318cb8 100644
--- a/bfd/elfcode.h
+++ b/bfd/elfcode.h
@@ -2022,6 +2022,7 @@ NAME(_bfd_elf,bfd_from_remote_memory)
}
nbfd->xvec = templ->xvec;
bim->size = high_offset;
+ bim->capacity = high_offset;
bim->buffer = contents;
nbfd->iostream = bim;
nbfd->flags = BFD_IN_MEMORY;
diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h
index fddcae44bc0..a28f3c2f32b 100644
--- a/bfd/libbfd-in.h
+++ b/bfd/libbfd-in.h
@@ -41,8 +41,10 @@ extern "C" {
struct bfd_in_memory
{
- /* Size of buffer. */
+ /* Logical size of buffer. */
bfd_size_type size;
+ /* Number of bytes allocated for buffer. */
+ bfd_size_type capacity;
/* Buffer holding contents of BFD. */
bfd_byte *buffer;
};
diff --git a/bfd/libbfd.h b/bfd/libbfd.h
index 038f2b56c65..80adcef01f8 100644
--- a/bfd/libbfd.h
+++ b/bfd/libbfd.h
@@ -47,8 +47,10 @@ extern "C" {
struct bfd_in_memory
{
- /* Size of buffer. */
+ /* Logical size of buffer. */
bfd_size_type size;
+ /* Number of bytes allocated for buffer. */
+ bfd_size_type capacity;
/* Buffer holding contents of BFD. */
bfd_byte *buffer;
};
diff --git a/bfd/opncls.c b/bfd/opncls.c
index a8fa6ebbf17..00d0b91ef66 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -1016,6 +1016,7 @@ bfd_make_writable (bfd *abfd)
abfd->iostream = bim;
/* bfd_write will grow these as needed. */
bim->size = 0;
+ bim->capacity = 0;
bim->buffer = 0;
abfd->flags |= BFD_IN_MEMORY;
diff --git a/bfd/peicode.h b/bfd/peicode.h
index e858632312f..0eb7ae38bef 100644
--- a/bfd/peicode.h
+++ b/bfd/peicode.h
@@ -872,6 +872,7 @@ pe_ILF_build_a_bfd (bfd * abfd,
ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
vars.bim->buffer = ptr;
vars.bim->size = ILF_DATA_SIZE;
+ vars.bim->capacity = ILF_DATA_SIZE;
if (ptr == NULL)
goto error_return;
diff --git a/bfd/xcofflink.c b/bfd/xcofflink.c
index f155d1c3dbb..2287958304b 100644
--- a/bfd/xcofflink.c
+++ b/bfd/xcofflink.c
@@ -4341,6 +4341,7 @@ bfd_xcoff_link_generate_rtinit (bfd *abfd,
return false;
bim->size = 0;
+ bim->capacity = 0;
bim->buffer = 0;
abfd->link.next = 0;
More information about the Binutils-cvs
mailing list