From patchwork Tue Jun 20 22:32:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 110703 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3978474vqr; Tue, 20 Jun 2023 15:32:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6obI+sof+XY8K7eb/cZp4MGoOkSoQnIQ8stDYIJDgnuHuCDbIW5XP+LBmDnP4mL0dwrbM+ X-Received: by 2002:a17:906:4793:b0:96f:2315:29fe with SMTP id cw19-20020a170906479300b0096f231529femr13111861ejc.0.1687300339896; Tue, 20 Jun 2023 15:32:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687300339; cv=none; d=google.com; s=arc-20160816; b=FGOuzPMws4FCjjv6ueimOAkVQ6D+YjRIpAawLcuhlXd+EkosONMLAv/FwWl3+qlewG c18zhecqWhhj5gHVRrFc70w3gFRdjWaSw/g1RlG33YhMuRVa2FUy2jq8Mad1zddPmX4u 60stXLJE55pTeNCVjBVmfowCqRb9Rx1RuPeF0+IF9zEANxokxE/WM5jLGqmF9hS4bZGC UhS98xXNHmnWaQQt5wnQ2Q42oP6jQ59fGfkWK/oWY4eiSJtWJTG72P/VmmklBIJeJBGW 36/7evHM41jm1+ZE3qD28y+/wW2KohHqopfgwKwEr3uX8l1VAXomGVxRLgOMTSdhqISP ca4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=5fRrCqM+zksFWDqj8EHo2hBXwMwYpbmUl8UBwA4PezE=; b=rAuiiCr8sz28qn/6pRmDVtz0zripJDP6H85nnrF1f39AucKWvOTsaZo7hhnTioCEd3 K2oed2rpBUIA9mIXUbF2wW0yypdGPgUMui6SirC+MXRD85aGW4Ok4kKsRpXE32jD5+gz LDX4a9MCNQkPOZ7tLMAukpFhVSaMSjZcC+keAYGaMoBLiKQAYu/RNw5OPBz+PvYs1wmu +3nMVACt1P2rfvyXe08AGZtbr/ASYq8JnksDny/OnLZCzm1C7agT9fumkhIBey1xW0gQ 3Q1CX0g01wxI4fQXKKERoO243XNgJBmuLZUVW5Pf3ly0YZTyAnARwGB+z/v6k+fwuSZ6 jDhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Kj8mgdCR; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id k12-20020a170906158c00b009888b71c367si1597140ejd.720.2023.06.20.15.32.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 15:32:19 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Kj8mgdCR; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D83523857C43 for ; Tue, 20 Jun 2023 22:32:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D83523857C43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687300336; bh=5fRrCqM+zksFWDqj8EHo2hBXwMwYpbmUl8UBwA4PezE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Kj8mgdCR/IAqX/bYCwE9oaxXWgPCAC1ruL33CCc+8u0FMhW4TMWq2Ohnca4tgRsaP ZvMBsRTYhAAkbjtR8gxTH02lpQSnqD8KWaRXpnRsg7jOr7IMQSN7iAoi/Ya6f2QotC prOtoLSy/fUz1AzuAWPWaOGdvPzEQhGKB/tC3vho= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 4D6C8385842B for ; Tue, 20 Jun 2023 22:32:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4D6C8385842B Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1b53b8465daso23029665ad.0 for ; Tue, 20 Jun 2023 15:32:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687300326; x=1689892326; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5fRrCqM+zksFWDqj8EHo2hBXwMwYpbmUl8UBwA4PezE=; b=SDr01f5arLggoF/4kpM9gR6Hw5M+pBv9xNFTHQG5DToTD7M7ekQfD/aMfg5UYYT3Ar zL7LrkhqtEDq/KZ9IiBeDX9nhGY5a/HNgDjx5O+4M0ptTZo/yQzA2t1vShcsRnvXN6vt qiEZr3V6SU2roEcfpoXowVMVjt1XMarrjv4zpsV+McsVjW8zKZCifPhS4hN4KktfQzSe YFJszpTFN9hNj0qum0e1L55rKwnUJ+Qsa37ojvIzh8MNmIrmppV51LAInJriSnSWoByU en9QSGwO5JCrKFR5od6A5r4b8jzaXtiAQqKATWQ3N8UmyR7hsPcTvebL4GERCqDy5fDX N0GA== X-Gm-Message-State: AC+VfDxQXhFDpqHnY6CmbByC2UkFxSq757FYiE3lwlb6OtfF9rn2RzEV B4Zu11GTggT0Si1EwgBdZPE+uWB7vkA= X-Received: by 2002:a17:902:ecc2:b0:1b5:561a:5c9a with SMTP id a2-20020a170902ecc200b001b5561a5c9amr5624223plh.39.1687300325821; Tue, 20 Jun 2023 15:32:05 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.123]) by smtp.gmail.com with ESMTPSA id n18-20020a170902969200b001b3a21fbb4fsm2112473plp.12.2023.06.20.15.32.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 15:32:05 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 2275D7401F3 for ; Tue, 20 Jun 2023 15:32:04 -0700 (PDT) To: binutils@sourceware.org Subject: [PATCH] x86: Free the symbol buffer and the relocation buffer after use Date: Tue, 20 Jun 2023 15:32:04 -0700 Message-Id: <20230620223204.629663-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3025.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Binutils" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769262641297310382?= X-GMAIL-MSGID: =?utf-8?q?1769262641297310382?= When --no-keep-memory is used, the symbol buffer and the relocation buffer aren't cached. When packing relative relocations, we may allocate a new symbol buffer and a new relocation buffer for each eligible section in an object file. If there are many sections, memory may be exhausted. In this case, we should free the symbol buffer and the relocation buffer after use. If symbol buffer entries are used to track relative relocations against local symbols for later use, the symbol buffer should be cached. PR ld/30566 * elfxx-x86.c (elf_x86_relative_reloc_record_add): Add an argument to inform caller if the symbol buffer should be kept. (_bfd_x86_elf_link_relax_section): Call _bfd_elf_link_info_read_relocs instead of _bfd_elf_link_read_relocs. Free the symbol buffer and the relocation buffer after use. Cache the symbol buffer if it is used. --- bfd/elfxx-x86.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c index 132fb791ac6..8e13a92e7f9 100644 --- a/bfd/elfxx-x86.c +++ b/bfd/elfxx-x86.c @@ -1013,7 +1013,7 @@ elf_x86_relative_reloc_record_add struct elf_x86_relative_reloc_data *relative_reloc, Elf_Internal_Rela *rel, asection *sec, asection *sym_sec, struct elf_link_hash_entry *h, - Elf_Internal_Sym *sym, bfd_vma offset) + Elf_Internal_Sym *sym, bfd_vma offset, bool *keep_symbuf_p) { bfd_size_type newidx; @@ -1057,6 +1057,8 @@ elf_x86_relative_reloc_record_add { relative_reloc->data[newidx].sym = sym; relative_reloc->data[newidx].u.sym_sec = sym_sec; + /* We must keep the symbol buffer since SYM will be used later. */ + *keep_symbuf_p = true; } relative_reloc->data[newidx].offset = offset; relative_reloc->data[newidx].address = 0; @@ -1086,6 +1088,8 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma *local_got_offsets; bool is_x86_64; bool unaligned_section; + bool return_status = false; + bool keep_symbuf = false; if (bfd_link_relocatable (info)) return true; @@ -1120,9 +1124,9 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED, /* Load the relocations for this section. */ internal_relocs = - _bfd_elf_link_read_relocs (abfd, input_section, NULL, - (Elf_Internal_Rela *) NULL, - info->keep_memory); + _bfd_elf_link_info_read_relocs (abfd, info, input_section, NULL, + (Elf_Internal_Rela *) NULL, + info->keep_memory); if (internal_relocs == NULL) return false; @@ -1163,11 +1167,13 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED, { isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; if (isymbuf == NULL) - isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, - symtab_hdr->sh_info, 0, - NULL, NULL, NULL); - if (isymbuf == NULL) - goto error_return; + { + isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, + symtab_hdr->sh_info, + 0, NULL, NULL, NULL); + if (isymbuf == NULL) + goto error_return; + } } isym = isymbuf + r_symndx; @@ -1267,7 +1273,8 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED, if (!elf_x86_relative_reloc_record_add (info, &htab->relative_reloc, irel, htab->elf.sgot, - sec, h, isym, offset)) + sec, h, isym, offset, + &keep_symbuf)) goto error_return; continue; @@ -1336,21 +1343,28 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED, ((unaligned_section || unaligned_offset) ? &htab->unaligned_relative_reloc : &htab->relative_reloc), - irel, input_section, sec, h, isym, offset)) + irel, input_section, sec, h, isym, offset, + &keep_symbuf)) goto error_return; } } input_section->relative_reloc_packed = 1; - return true; + return_status = true; error_return: if ((unsigned char *) isymbuf != symtab_hdr->contents) - free (isymbuf); + { + /* Cache the symbol buffer if it must be kept. */ + if (keep_symbuf) + symtab_hdr->contents = (unsigned char *) isymbuf; + else + free (isymbuf); + } if (elf_section_data (input_section)->relocs != internal_relocs) free (internal_relocs); - return false; + return return_status; } /* Add an entry to the 64-bit DT_RELR bitmap. */