From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id 406E23857811 for ; Wed, 13 Mar 2024 16:08:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 406E23857811 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 406E23857811 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1031 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710346104; cv=none; b=JpGYXw7DfIi/EwcVJ3AUq5+6Q4y3yZAYXRpuJnL+bdrILJ+sAjNQwmtDsVEhFHcgJkoNGYdj+wjQZqBOHsDCojEEd/qAgaxbzUVSU8Jm6xBJ6NTYmGbugT6NYUnWxjkpxutZUNSlW65caTQddYrl1OhTjLDyaeeovh6yfBfoAsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710346104; c=relaxed/simple; bh=JR+6xDfDVwkPugfTfL8aqCQ3xFE7vlEGvXsqzyT8/7g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rMAN8LA5KDUneFA2c5ArRtYVXtGBpudy57cqSGEX3fQsQDO0xmNzLRrBsq4z6kwr+f2lObZKsxftFqM1KYIGz81tanZMc1oj0oRPGr1W8vgRBK5AoCRdKg6OveQ5majNQVol/Bn9dS0Q+1TwqkB8RpWX3ya2zi9jOAHePvGe/d4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-29c71c6e20cso39394a91.2 for ; Wed, 13 Mar 2024 09:08:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710346100; x=1710950900; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ho9Tg0oPKZ+g3xJLjgMHqz2KTQqbfPx+2ROSRr0YK+U=; b=M7YBf7UeQBzgm7mwFjLTnu9ntRX3tqpZnOI40w2fWVOoTXjzFFsTRcTa3ekHffQBfS sMX9KCoehUZ/AEjXMTCJ59535cbgs/0h/qe/DHRJ41u/a18MEVcgkGk5jgelvHhetrht GfXJ6jb2eTZyMIpnBdbMpbw+fQKXk8TxTR8Ta/LhP2wY+NO0hEeNZqWUQ1JkRT5zX16Q HKKVf46w33MiElfbWEu6JARtFFSdfggM4t80yzskQ8E+U9gSgJSyLspK0snIoMneKQVM ekOqS3RLHdr0xTtGKy37Tc7ann7TdeEk7aqdtCM0Wx2Ydi0ubq+d62NotPS+DNmS4q2O G1ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710346100; x=1710950900; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ho9Tg0oPKZ+g3xJLjgMHqz2KTQqbfPx+2ROSRr0YK+U=; b=lef1zQZBq3LxIEetpNBI3/8DIUKADLIgy6mBGzp5Gsnjic3pnn5CbJnm82sIs/G1Ux 6OFFN1Z6Yh1F4sfg1lZmqnHsepPFqA+q56k4Shz8VZ4bzfPj+UPEk6feHLWqq23k5i7h zHEvvjZveiYP9UG63xVawyu/wQcJViZjxwFPOx/x2RV5ScOG130EH+Up0r6KxtvzY9fl wGR0OhNV2wrY8dCegJUbu1vK7rp19gwl1/vOW1D87KKW4780Ui4MI19l86iABLFoMgAD x6GWwcHbBckvHVdds2SkUbWeeVbECFvClbXwCmv+U0gbr++YWsDwvgalqAfDYrDYGmKu CG4A== X-Gm-Message-State: AOJu0Yw3A8NqMRdmQAhei1Y4jQPuokL6ZAdPVLf+bnlPAg6xz9R0hDkX HffjToVMgU3ge/xQz91VAVE7EGqlbhLvZ5vFQ6/KahMTmwSIrKhTnrV0q4V7 X-Google-Smtp-Source: AGHT+IGNiWLcgdDllWBE2hQXd+AlfowO2Sfo2ydQ1WEfIRZAqhI379tsD6jz0SF4FloyVZj6H46aBg== X-Received: by 2002:a17:90a:f305:b0:299:63fe:3a27 with SMTP id ca5-20020a17090af30500b0029963fe3a27mr9687813pjb.19.1710346100187; Wed, 13 Mar 2024 09:08:20 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.58.89.72]) by smtp.gmail.com with ESMTPSA id q14-20020a17090aa00e00b0029bbb9690d7sm1534790pjp.18.2024.03.13.09.08.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 09:08:19 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 878C474071D; Wed, 13 Mar 2024 09:08:15 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Cc: goldstein.w.n@gmail.com, sam@gentoo.org, amodra@gmail.com Subject: [PATCH v9 6/6] elf: Add _bfd_elf_link_m[un]map_section_contents Date: Wed, 13 Mar 2024 09:08:15 -0700 Message-ID: <20240313160815.665818-7-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240313160815.665818-1-hjl.tools@gmail.com> References: <20240313160815.665818-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3019.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_ABUSEAT,RCVD_IN_DNSWL_NONE,RCVD_IN_SBL_CSS,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 List-Id: To copy input section contents, add _bfd_elf_link_mmap_section_contents and _bfd_elf_link_munmap_section_contents to mmap in the input sections. _bfd_elf_link_free_section_contents is added for flinfo.external_relocs, which is nop when mmap is used since flinfo.external_relocs is unused. When mmap isn't used, _bfd_elf_link_munmap_section_contents is also nop. * elf-bfd.h (_bfd_elf_link_mmap_section_contents): New. (_bfd_elf_link_munmap_section_contents): Likewise. (_bfd_elf_link_free_section_contents): Likewise. * elf.c (elf_mmap_section_contents): New. (_bfd_elf_mmap_section_contents): Use it. (_bfd_elf_link_mmap_section_contents): New. (_bfd_elf_munmap_section_contents): Never free the cached section contents. * elflink.c (elf_link_input_bfd): Call _bfd_elf_link_mmap_section_contents instead of bfd_get_full_section_contents. Call _bfd_elf_link_munmap_section_contents to munmap the section contents. (elf_final_link_free): Use _bfd_elf_link_free_section_contents, instead of free, on flinfo->contents. (bfd_elf_final_link): Don't allocate memory for section contents. --- bfd/elf-bfd.h | 10 ++++++++++ bfd/elf.c | 43 +++++++++++++++++++++++++++++++++---------- bfd/elflink.c | 10 +++++++--- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index b9ec590cd4e..4b9dc1c72b8 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -3143,6 +3143,16 @@ extern bool _bfd_elf_mmap_section_contents (bfd *abfd, asection *section, bfd_byte **buf); extern void _bfd_elf_munmap_section_contents (asection *, void *); +extern bool _bfd_elf_link_mmap_section_contents + (bfd *abfd, asection *section, bfd_byte **buf); +#ifdef USE_MMAP +#define _bfd_elf_link_munmap_section_contents(sec, mem) \ + _bfd_elf_munmap_section_contents (sec, mem) +#define _bfd_elf_link_free_section_contents(ptr) +#else +#define _bfd_elf_link_munmap_section_contents(sec, mem) +#define _bfd_elf_link_free_section_contents(ptr) free (ptr) +#endif extern bool _bfd_elf_link_keep_memory (struct bfd_link_info *); diff --git a/bfd/elf.c b/bfd/elf.c index 3a6c07af6c1..65d01aabfb1 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -14416,10 +14416,12 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) return result; } -/* Mmap in section contents. */ +/* Mmap in section contents. If LINK is false, set *BUF to NULL before + calling bfd_get_full_section_contents. */ -bool -_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) +static bool +elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf, + bool link) { #ifdef USE_MMAP const struct elf_backend_data *bed = get_elf_backend_data (abfd); @@ -14446,13 +14448,34 @@ _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) } } #endif - *buf = NULL; + /* NB: When this is called from elf_link_input_bfd, LINK is true, + *BUF is set to the preallocated buffer if USE_MMAP is undefined + and *BUF is set to NULL if USE_MMAP is defined. */ + if (!link) + *buf = NULL; bool ret = bfd_get_full_section_contents (abfd, sec, buf); if (ret && sec->mmapped_p) *buf = sec->contents; return ret; } +/* Mmap in section contents. */ + +bool +_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) +{ + return elf_mmap_section_contents (abfd, sec, buf, false); +} + +/* Mmap in the full section contents for linking. */ + +bool +_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec, + bfd_byte **buf) +{ + return elf_mmap_section_contents (abfd, sec, buf, true); +} + /* Munmap section contents. */ void @@ -14464,15 +14487,15 @@ _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED, if (contents == NULL) return; + /* elf_mmap_section_contents may return the previously mapped section + contents. Munmap the section contents only if they haven't been + cached. */ + if (elf_section_data (sec)->this_hdr.contents == contents) + return; + #ifdef USE_MMAP if (sec->mmapped_p) { - /* _bfd_elf_mmap_section_contents may return the previously - mapped section contents. Munmap the section contents only - if they haven't been cached. */ - if (elf_section_data (sec)->this_hdr.contents == contents) - return; - /* When _bfd_elf_mmap_section_contents returns CONTENTS as malloced, CONTENTS_ADDR is set to NULL. */ if (elf_section_data (sec)->contents_addr != NULL) diff --git a/bfd/elflink.c b/bfd/elflink.c index 7cb9269f101..7b781cb79ab 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11463,7 +11463,8 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) else { contents = flinfo->contents; - if (! bfd_get_full_section_contents (input_bfd, o, &contents)) + if (! _bfd_elf_link_mmap_section_contents (input_bfd, o, + &contents)) return false; } @@ -12021,6 +12022,9 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) } break; } + + /* Munmap the section contents for each input section. */ + _bfd_elf_link_munmap_section_contents (o, contents); } return true; @@ -12316,7 +12320,7 @@ elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) if (flinfo->symstrtab != NULL) _bfd_elf_strtab_free (flinfo->symstrtab); - free (flinfo->contents); + _bfd_elf_link_free_section_contents (flinfo->contents); free (flinfo->external_relocs); free (flinfo->internal_relocs); free (flinfo->external_syms); @@ -12748,6 +12752,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) && bed->elf_backend_elfsym_local_is_section (abfd)) symtab_hdr->sh_info = bfd_get_symcount (abfd); +#ifndef USE_MMAP /* Allocate some memory to hold information read in from the input files. */ if (max_contents_size != 0) @@ -12757,7 +12762,6 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) goto error_return; } -#ifndef USE_MMAP if (max_external_reloc_size != 0) { flinfo.external_relocs = bfd_malloc (max_external_reloc_size); -- 2.44.0