[PATCH v2] bfd: Grow in-memory BFD buffers geometrically

Jan Beulich jbeulich@suse.com
Fri Nov 13 09:47:45 GMT 2026


On 06.11.2026 21:33, Oleg Tolmatcev wrote:
> 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.
> 
> 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.

Trying to apply this to run through a full round of testing, I find that ...

> --- a/bfd/coff-bfd.c
> +++ b/bfd/coff-bfd.c
> @@ -241,6 +241,7 @@ import_make_member (bfd *parent, const char *name,
>  
>    bim->buffer = data;
>    bim->size = size;
> +  bim->capacity = size;
>  
>    abfd->format = bfd_unknown;
>    abfd->tdata.any = NULL;

... the function being patched here doesn't exist at all, and ...

> --- a/bfd/peicode.h
> +++ b/bfd/peicode.h
> @@ -881,6 +881,7 @@ pe_ILF_build_a_bfd (bfd *	    abfd,
>    ptr = (bfd_byte *) bfd_zmalloc (raw_space + ILF_DATA_SIZE);
>    vars.bim->buffer = ptr;
>    vars.bim->size   = raw_space + ILF_DATA_SIZE;
> +  vars.bim->capacity = raw_space + ILF_DATA_SIZE;
>    if (ptr == NULL)
>      goto error_return;

... this chunk of code also looks different (no raw_space anywhere in
sight).

Please can you make sure that you send patches which actually apply?
This time I'll make the necessary adjustments, but this isn't a promise
that I would continue doing so.

Jan


More information about the Binutils mailing list