[PATCH v2] bfd: Grow in-memory BFD buffers geometrically
Jan Beulich
jbeulich@suse.com
Fri Nov 13 09:15:35 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.
Okay, with one remark:
> @@ -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 = EINVAL;
I realize you only re-indent this line, yet as it needs changing anyway,
wouldn't it make sense to switch to ENOMEM? It's a buffer allocation which
failed, it's not that the requested position would be invalid. Yes, standard
lseek() isn't supposed to return ENOMEM, yet the reasons for it to return
EINVAL also don't apply here.
Jan
More information about the Binutils
mailing list