From patchwork Fri Jan 27 07:02:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 49111 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp691320wrn; Thu, 26 Jan 2023 23:02:49 -0800 (PST) X-Google-Smtp-Source: AK7set8H2wH22t4D3kNVjqfJuVGc+5EetHIZy21iGMbt5+l/2NeddDspwMe5TEuqPRb/eDRmIHhW X-Received: by 2002:a17:906:960d:b0:878:4dd1:5161 with SMTP id s13-20020a170906960d00b008784dd15161mr6527751ejx.11.1674802969741; Thu, 26 Jan 2023 23:02:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674802969; cv=none; d=google.com; s=arc-20160816; b=0bnYBoaLPqPBLmndwzBrw41+2yJQddoDMd/tYezMqWdRi8gkKQvLCwP3AmgBb1mijZ YwqhiD/eofVBUUQl42kflHK2W4uCwUwOWYW6IB0ZEHAkz9qs4JSJOLc9eqIgn/xj5Kj6 dKvV8nhpT8Da294J2M0Igz2lrhajkdsKcUtYHBAJ6eZYrulnqBl9tZ91Ryd5LJXZNyAr VmSuLRKC3enexKvosp2cFlsMI5e7RXe4ov/fEkXWDp+2SrZbgcHM7OJV9P+ft7r8DI+3 miEbRlisyJ9oXd27j9bexG2mTmRfdQ7Zbam2uoaz6MjRNIusUQ7u5gvS3fd3JK4LjUMh Puaw== 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-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=6debiviFxDDihvIMp8ZJFsUFdjU/hUfBfJMkMlOosBA=; b=F5JMnju8NIJgNtD1S1pRLSxSH1pisLN30P0o3eQz2WWWlbAVmZRQe0EmcDoXLW7WNb JCHXuYtA2wNMNVOF37AZKfUdNEIfq2Ev2krfZ8WbP8l0lpD3AGdxzKPaTqye+WDKMVse AH2tJEuFg3ehR9XEwsANvtffWsuFQkOYz90mnHGNnt5uKkJegR/O5oRL3qu0e2k0spXk bBfozfJsT8FXnHHnr8Cf02KWOSX7Q0wtXnL4NFd2pzB8uJ3HmMEupivOi49MOlgkstJL W9wHQ8UeY0AZdIvI5yE7HqZB/Ptbrih9zf2erB3UF6OdpXB7B9gmtv7b64q4l75fBD1E OdhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="u4N/Aos7"; 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 ev3-20020a17090729c300b0087848ecf38fsi3403696ejc.290.2023.01.26.23.02.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 23:02:49 -0800 (PST) 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="u4N/Aos7"; 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 236FE385842C for ; Fri, 27 Jan 2023 07:02:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 236FE385842C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674802968; bh=6debiviFxDDihvIMp8ZJFsUFdjU/hUfBfJMkMlOosBA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=u4N/Aos7rwfSGKZaBQ8VEArGBojIEzSjg7iDyaauxOmK08nZK5NcxVEDz0MOAImOd i4JLG4fgRGXI/sWSiOFuNMU87M3IzdIP/NSUndoOaBeRwbZtQBV1jKUJRH9nnajaWA +tm01NDPptBt+puPMPR6fDmorrEgYu03tiYGPd0g= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id 813193858C60 for ; Fri, 27 Jan 2023 07:02:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 813193858C60 Received: by mail-pg1-x52f.google.com with SMTP id 7so2673796pga.1 for ; Thu, 26 Jan 2023 23:02:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6debiviFxDDihvIMp8ZJFsUFdjU/hUfBfJMkMlOosBA=; b=VXzZ8rLEza/mty9+1lL7Gk1HrCwNVdzXirIJ6bZm012KOsVu7NAWdW14P4HwE7sIx0 buKyYBpubFLBH+OeoJR3y9xMSlqUzRRKO4uT/RxopeQN1MrLsI+bwq4DHk3eXPFm2ViB pwxIdLL1sdJIqNSO0XnH+renXz4iYNUXg1+Mf5AS2tlGi5hYuTqVaqZSPluRkqgk6400 zwn7ObpyRCQNMvzjsZA7f98oggtreeCe0AUEDNR5SmKNJvrg/sIujBwfJ1hr5AaATE97 zZuhHKIVwjWh6JK27H2R3iI7pICHZ9K8GoFGWE+RjNhlzmmokP7hUG9IxiPMcOq4pUox Yu5Q== X-Gm-Message-State: AFqh2koohBjbM5cyFsuGBUQyL+xvKwaOvLxIMa74F4lkYkW9Mq/fvkJ5 7dxlYj0dj0XfjUaKyxVLRKTwhPP4pHA= X-Received: by 2002:aa7:91c1:0:b0:58a:fddd:9b1d with SMTP id z1-20020aa791c1000000b0058afddd9b1dmr34916677pfa.10.1674802957271; Thu, 26 Jan 2023 23:02:37 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:fb44:1882:a135:67f]) by smtp.gmail.com with ESMTPSA id c74-20020a621c4d000000b005895f9657ebsm1922019pfc.70.2023.01.26.23.02.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 23:02:37 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id DF6461142E33; Fri, 27 Jan 2023 17:32:34 +1030 (ACDT) Date: Fri, 27 Jan 2023 17:32:34 +1030 To: binutils@sourceware.org Subject: Call bfd_close_all_done in ld_cleanup Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.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 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: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra 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?1756158198758833219?= X-GMAIL-MSGID: =?utf-8?q?1756158198758833219?= This is similar to "Call bfd_close_all_done in output_file_close", but with some code tidying in the pe/pep write_build_id functions. write_build_id is passed the output bfd as its parameter, so there is no need to go looking for the output bfd via link_info (and doing so will no longer work since I clear link_info.output_bfd before calling bfd_close). * emultempl/pe.em (write_build_id): Rename t to td. Formatting. Don't access pe_data(link_info.output_bfd), use td instead. (setup_build_id): Rename t to td. Formatting. * emultempl/pep.em: As for pe.em. * ldmain.c (ld_cleanup): Call bfd_close_all_done on linker bfds. (main): Clear link_info.output_bfd when closing. diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 7e1866a23c7..55412d6ef9e 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1300,7 +1300,7 @@ pecoff_checksum_contents (bfd *abfd, static bool write_build_id (bfd *abfd) { - struct pe_tdata *t = pe_data (abfd); + struct pe_tdata *td = pe_data (abfd); asection *asec; struct bfd_link_order *link_order = NULL; unsigned char *contents; @@ -1316,7 +1316,7 @@ write_build_id (bfd *abfd) { if (l->type == bfd_indirect_link_order) { - if (l->u.indirect.section == t->build_id.sec) + if (l->u.indirect.section == td->build_id.sec) { link_order = l; break; @@ -1335,15 +1335,16 @@ write_build_id (bfd *abfd) return true; } - if (t->build_id.sec->contents == NULL) - t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); - contents = t->build_id.sec->contents; + if (td->build_id.sec->contents == NULL) + td->build_id.sec->contents = xmalloc (td->build_id.sec->size); + contents = td->build_id.sec->contents; - build_id_size = compute_build_id_size (t->build_id.style); + build_id_size = compute_build_id_size (td->build_id.style); build_id = xmalloc (build_id_size); - generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size); + generate_build_id (abfd, td->build_id.style, pecoff_checksum_contents, + build_id, build_id_size); - bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase; + bfd_vma ib = td->pe_opthdr.ImageBase; #ifdef PDB_H if (pdb_name) @@ -1390,10 +1391,12 @@ write_build_id (bfd *abfd) cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE; cvinfo.Age = 1; - /* Zero pad or truncate the generated build_id to fit in the CodeView record. */ + /* Zero pad or truncate the generated build_id to fit in the + CodeView record. */ memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH); - memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH) - ? CV_INFO_SIGNATURE_LENGTH : build_id_size); + memcpy (&(cvinfo.Signature), build_id, + (build_id_size > CV_INFO_SIGNATURE_LENGTH + ? CV_INFO_SIGNATURE_LENGTH : build_id_size)); free (build_id); @@ -1403,9 +1406,9 @@ write_build_id (bfd *abfd) return 0; /* Record the location of the debug directory in the data directory. */ - pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress - = asec->vma - ib + link_order->offset; - pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size + td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress + = asec->vma - ib + link_order->offset; + td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY); return true; @@ -1429,17 +1432,17 @@ setup_build_id (bfd *ibfd) s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags); if (s != NULL) { - struct pe_tdata *t = pe_data (link_info.output_bfd); - t->build_id.after_write_object_contents = &write_build_id; - t->build_id.style = emit_build_id; - t->build_id.sec = s; + struct pe_tdata *td = pe_data (link_info.output_bfd); + td->build_id.after_write_object_contents = &write_build_id; + td->build_id.style = emit_build_id; + td->build_id.sec = s; /* Section is a fixed size: One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW, pointing at a CV_INFO_PDB70 record containing the build-id, followed by PdbFileName if relevant. */ - s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY) - + sizeof (CV_INFO_PDB70) + 1; + s->size = (sizeof (struct external_IMAGE_DEBUG_DIRECTORY) + + sizeof (CV_INFO_PDB70) + 1); #ifdef PDB_H if (pdb_name) diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index 613a2c70531..2a3fd0e6ea8 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1302,7 +1302,7 @@ pecoff_checksum_contents (bfd *abfd, static bool write_build_id (bfd *abfd) { - struct pe_tdata *t = pe_data (abfd); + struct pe_tdata *td = pe_data (abfd); asection *asec; struct bfd_link_order *link_order = NULL; unsigned char *contents; @@ -1318,7 +1318,7 @@ write_build_id (bfd *abfd) { if (l->type == bfd_indirect_link_order) { - if (l->u.indirect.section == t->build_id.sec) + if (l->u.indirect.section == td->build_id.sec) { link_order = l; break; @@ -1337,15 +1337,16 @@ write_build_id (bfd *abfd) return true; } - if (t->build_id.sec->contents == NULL) - t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); - contents = t->build_id.sec->contents; + if (td->build_id.sec->contents == NULL) + td->build_id.sec->contents = xmalloc (td->build_id.sec->size); + contents = td->build_id.sec->contents; - build_id_size = compute_build_id_size (t->build_id.style); + build_id_size = compute_build_id_size (td->build_id.style); build_id = xmalloc (build_id_size); - generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size); + generate_build_id (abfd, td->build_id.style, pecoff_checksum_contents, + build_id, build_id_size); - bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase; + bfd_vma ib = td->pe_opthdr.ImageBase; #ifdef PDB_H if (pdb_name) @@ -1372,7 +1373,7 @@ write_build_id (bfd *abfd) struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents; _bfd_XXi_swap_debugdir_out (abfd, &idd, ext); - /* Write the debug directory enttry */ + /* Write the debug directory entry. */ if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0) return 0; @@ -1392,10 +1393,12 @@ write_build_id (bfd *abfd) cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE; cvinfo.Age = 1; - /* Zero pad or truncate the generated build_id to fit in the CodeView record. */ + /* Zero pad or truncate the generated build_id to fit in the + CodeView record. */ memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH); - memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH) - ? CV_INFO_SIGNATURE_LENGTH : build_id_size); + memcpy (&(cvinfo.Signature), build_id, + (build_id_size > CV_INFO_SIGNATURE_LENGTH + ? CV_INFO_SIGNATURE_LENGTH : build_id_size)); free (build_id); @@ -1405,9 +1408,9 @@ write_build_id (bfd *abfd) return 0; /* Record the location of the debug directory in the data directory. */ - pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress - = asec->vma - ib + link_order->offset; - pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size + td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress + = asec->vma - ib + link_order->offset; + td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY); return true; @@ -1431,10 +1434,10 @@ setup_build_id (bfd *ibfd) s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags); if (s != NULL) { - struct pe_tdata *t = pe_data (link_info.output_bfd); - t->build_id.after_write_object_contents = &write_build_id; - t->build_id.style = emit_build_id; - t->build_id.sec = s; + struct pe_tdata *td = pe_data (link_info.output_bfd); + td->build_id.after_write_object_contents = &write_build_id; + td->build_id.style = emit_build_id; + td->build_id.sec = s; /* Section is a fixed size: One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW, diff --git a/ld/ldmain.c b/ld/ldmain.c index 9290a189b0d..8c2fc9b8d8c 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -212,7 +212,14 @@ write_dependency_file (void) static void ld_cleanup (void) { - bfd_cache_close_all (); + bfd *ibfd, *inext; + if (link_info.output_bfd) + bfd_close_all_done (link_info.output_bfd); + for (ibfd = link_info.input_bfds; ibfd; ibfd = inext) + { + inext = ibfd->link.next; + bfd_close_all_done (ibfd); + } #if BFD_SUPPORTS_PLUGINS plugin_call_cleanup (); #endif @@ -559,7 +566,9 @@ main (int argc, char **argv) } else { - if (!bfd_close (link_info.output_bfd)) + bfd *obfd = link_info.output_bfd; + link_info.output_bfd = NULL; + if (!bfd_close (obfd)) einfo (_("%F%P: %s: final close failed: %E\n"), output_filename); /* If the --force-exe-suffix is enabled, and we're making an @@ -621,7 +630,7 @@ main (int argc, char **argv) fflush (stderr); } - /* Prevent ld_cleanup from doing anything, after a successful link. */ + /* Prevent ld_cleanup from deleting the output file. */ output_filename = NULL; xexit (0);