Bug 18028 - 32-bit ld runs out of memory when linking 32-bit clang with debug info
Summary: 32-bit ld runs out of memory when linking 32-bit clang with debug info
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.37
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-02-25 17:59 UTC by H.J. Lu
Modified: 2025-09-03 17:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2015-02-25 17:59:27 UTC
On Linux/x86, 32-bit ld runs out of memeory when linking 32-bit clang with
debug info:

[hjl@gnu-mic-2 build-i686-linux]$ /usr/local32/bin/ld.bfd `cat args`
/usr/local32/bin/ld.bfd: final link failed: Memory exhausted
[hjl@gnu-mic-2 build-i686-linux]$ /usr/local32/bin/ld.bfd -s `cat args`
[hjl@gnu-mic-2 build-i686-linux]$ file Debug+Asserts/bin/clang
Debug+Asserts/bin/clang: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=670f5994a01c0d03c089dffd439e338233638fd1, stripped
[hjl@gnu-mic-2 build-i686-linux]$
Comment 1 Sourceware Commits 2021-07-07 14:45:47 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7a30ac441a4e776e68399cb916c4731b5192e931

commit 7a30ac441a4e776e68399cb916c4731b5192e931
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jul 6 06:21:54 2021 -0700

    ld: Check archive only for archive member
    
    Since plugin_maybe_claim calls bfd_close on the original input BFD if it
    isn't an archive member, pass NULL to bfd_plugin_close_file_descriptor
    to indicate that the BFD isn't an archive member.
    
    bfd/
    
            PR ld/18028
            * plugin.c (bfd_plugin_close_file_descriptor): Check archive
            only of abfd != NULL.
            (try_claim): Pass NULL to bfd_plugin_close_file_descriptor if
            it isn't an archive member.
    
    ld/
    
            PR ld/18028
            * plugin.c (plugin_input_file): Add comments for abfd and ibfd.
            (plugin_object_p): Set input->ibfd to NULL if it isn't an
            archive member.
Comment 2 Sourceware Commits 2021-07-07 14:50:49 UTC
The binutils-2_37-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f9b771014915eebbd20791870ffd175b9156ce42

commit f9b771014915eebbd20791870ffd175b9156ce42
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Jul 7 07:47:00 2021 -0700

    ld: Check archive only for archive member
    
    Since plugin_maybe_claim calls bfd_close on the original input BFD if it
    isn't an archive member, pass NULL to bfd_plugin_close_file_descriptor
    to indicate that the BFD isn't an archive member.
    
    bfd/
    
            PR ld/18028
            * plugin.c (bfd_plugin_close_file_descriptor): Check archive
            only of abfd != NULL.
            (try_claim): Pass NULL to bfd_plugin_close_file_descriptor if
            it isn't an archive member.
    
    ld/
    
            PR ld/18028
            * plugin.c (plugin_input_file): Add comments for abfd and ibfd.
            (plugin_object_p): Set input->ibfd to NULL if it isn't an
            archive member.
    
    (cherry picked from commit 7a30ac441a4e776e68399cb916c4731b5192e931)
Comment 3 Sourceware Commits 2021-07-08 17:02:29 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a8dde0a2114f87bcdc19946aeab26788f5eae1b7

commit a8dde0a2114f87bcdc19946aeab26788f5eae1b7
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Feb 27 12:22:58 2018 -0800

    ld: Limit cache size and add --max-cache-size=SIZE
    
    When link_info.keep_memory is true, linker caches the relocation
    information and symbol tables of input files in memory.  When there
    are many input files with many relocations, we may run out of memory.
    Add --max-cache-size=SIZE to set the maximum cache size.
    
    bfd/
    
            PR ld/18028
            * bfd.c (bfd): Add alloc_size.
            * elf-bfd.h (_bfd_elf_link_info_read_relocs): New.
            * elf32-i386.c (elf_i386_check_relocs): Use _bfd_link_keep_memory.
            Update cache_size.
            * elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
            * elflink.c (_bfd_elf_link_read_relocs): Renamed to ...
            (_bfd_elf_link_info_read_relocs): This.  Update cache_size.
            (_bfd_elf_link_read_relocs): New.
            (_bfd_elf_link_check_relocs): Call _bfd_elf_link_info_read_relocs
            instead of _bfd_elf_link_read_relocs.
            (elf_link_add_object_symbols): Likewise.
            (elf_link_input_bfd): Likewise.
            (init_reloc_cookie_rels): Likewise.
            (init_reloc_cookie): Update cache_size.  Call
            _bfd_elf_link_info_read_relocs instead of
            _bfd_elf_link_read_relocs.
            (link_info_ok): New.
            (elf_gc_smash_unused_vtentry_relocs): Updated.  Call
            _bfd_elf_link_info_read_relocs instead of
            _bfd_elf_link_read_relocs.
            (bfd_elf_gc_sections): Use link_info_ok.  Pass &link_info_ok
            to elf_gc_smash_unused_vtentry_relocs.
            * libbfd-in.h (_bfd_link_keep_memory): New.
            * linker.c (_bfd_link_keep_memory): New.
            * opncls.c (bfd_alloc): Update alloc_size.
            * bfd-in2.h: Regenerated.
            * libbfd.h: Likewise.
    
    include/
    
            PR ld/18028
            * bfdlink.h (bfd_link_info): Add cache_size and max_cache_size.
    
    ld/
    
            PR ld/18028
            * NEWS: Mention --max-cache-size=SIZE.
            * ld.texi: Document --max-cache-size=SIZE.
            * ldlex.h (option_values): Add OPTION_MAX_CACHE_SIZE.
            * ldmain.c: (main): Set link_info.max_cache_size to -1.
            * lexsup.c (ld_options): Add --max-cache-size=SIZE.
            (parse_args): Support OPTION_MAX_CACHE_SIZE.
            * testsuite/ld-bootstrap/bootstrap.exp: Add test for
            --max-cache-size=-1.
Comment 4 Sam James 2025-05-14 06:56:16 UTC
Fixed in 2.37.