From patchwork Fri Mar 15 12:24:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 87241 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EDEF4385DC0E for ; Fri, 15 Mar 2024 12:25:52 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id E12073857B8E for ; Fri, 15 Mar 2024 12:24:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E12073857B8E 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 E12073857B8E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710505473; cv=none; b=J1/zc5TYWqlIdLhtPWjJiKkn+kXVeAQB76+ZLlmVCjj35haXvCiA4hf03IR2Uk3BK003a7h5ocPoJaoSfyDs95HSIcnmVvezmxaIej6fh6yopqA4nsjhac1F3on5iqlvV6ez4j8ZU92Zdq8UEbDcceFYuoOZ5iPRJsOiFSJri8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710505473; c=relaxed/simple; bh=eeCa47Jj0zR3TGDD+KItQpICX0XkeyFWWixSGkuqbOA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=na3pAnVr9ptLf1Pthprx+e2rv/q8jZXzReCeKL6hliFGCfVOHyn7dRMZvvCmX/PtdIOz9TNThjpLsBznLU+3aXliM+ucczl6KZn9WffWCraKBLGXWiETtqZl1CPoVbJcWE26CEz6c9fk1nr8PwgIqDq8lHHk2wYbPWFBONOmIvg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1dca3951ad9so14583465ad.3 for ; Fri, 15 Mar 2024 05:24:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710505469; x=1711110269; 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=ko4/QQ1fjofafjkuGyJkLrRwvvanIEOrs98j6+dER6U=; b=JwF9l4wP8zNoiHU2F35KG8PzgBokemE3ALY2Vg5no75txpcg2LzT7kYfvhf0Icg9Ue TCsD01foTJvDUlRJ2H/L0DBsIKCLYUNejkNfzseOd9fTBdPMu8k3M+QZa27546jRu1KE lluG9dZRgTD4SllSABqK27h/4+Fkkxr9qcAC+6xieKaHR+zhB1cFzdhyoBTfy1EAx+Ub AlZkdERDXWJjjuPCUgKV51AWITJv+5gcVFBoa+Xk4PWCEHDxP1CC0X6bfXY6xJqMQfnn rm7I6YWTdDDZrATPqbADTEXtP1ZxwenGIRJkDkUBydyP+lW5ERAeGWsLqjqNKP/l2Tjp lFPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710505469; x=1711110269; 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=ko4/QQ1fjofafjkuGyJkLrRwvvanIEOrs98j6+dER6U=; b=fEUTB4EL337175W45IlX9Q/D9a57WD/JSxvSIBHnLGt5UYv63svCcDLDL2d25JQa+A zfUbDu0F0vzHtoDQjf8/mPKUNJn7w6Qt1MIB2Z7zWl6Ai0diDtYMOdGH2O1TSTxJWOUK HNHQLRt62Lif9AmRztEbZOJ8M7zeCEnPDFYLP2n6iKmvIj2Hhbhbjx5+varE/JwUiYYQ a+A9YKpic7b58GszfllB0wOPwJ6eay2i9mCKv/6tg+Ccb8MiJUX2IJ3IumLYcxoYmAae MaKlZFqXVOrgXFhdT9QccJ9wU2bzWXEgt2GeZjYBMAPUg5XrF063gP3TWYPKJ8RUjvno iuPA== X-Gm-Message-State: AOJu0YxVLgzX5YmhQk2GLFbAzvgKWe5PvSzydin/IWsKCickvUFZ5TPt +QeQvpwaw5yOB8jDijW8dwExYDJCGoA7sNII7zjMIPbhS9q3jn7uPLWj0eAq X-Google-Smtp-Source: AGHT+IFBifb489H+ceZ8Rl8a3Pe9V9bTPKAa1PzldItkjGx2noVtWBFSwXxTA9+tGUStQq+dtiI6lA== X-Received: by 2002:a17:902:d552:b0:1dd:e15d:6bb6 with SMTP id z18-20020a170902d55200b001dde15d6bb6mr5459413plf.17.1710505468875; Fri, 15 Mar 2024 05:24:28 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.58.89.72]) by smtp.gmail.com with ESMTPSA id n3-20020a170902e54300b001def088c036sm1343565plf.19.2024.03.15.05.24.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 05:24:27 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id C08FF74071D; Fri, 15 Mar 2024 05:24:23 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Cc: goldstein.w.n@gmail.com, sam@gentoo.org, amodra@gmail.com Subject: [PATCH v11 6/6] elf: Add _bfd_elf_link_m[un]map_section_contents Date: Fri, 15 Mar 2024 05:24:23 -0700 Message-ID: <20240315122423.1963285-7-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240315122423.1963285-1-hjl.tools@gmail.com> References: <20240315122423.1963285-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3020.9 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, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org 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. * elf-bfd.h (_bfd_elf_link_mmap_section_contents): New. (_bfd_elf_link_munmap_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_link_munmap_section_contents): Likewise. * 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. (bfd_elf_final_link): When mmap is used, initialize max_contents_size to _bfd_minimum_mmap_size and increase it for compressed or linker created sections or sections whose rawsize != size. --- bfd/elf-bfd.h | 4 ++++ bfd/elf.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++---- bfd/elflink.c | 33 +++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 12 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index b9ec590cd4e..c386af3549f 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -3143,6 +3143,10 @@ 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); +extern void _bfd_elf_link_munmap_section_contents + (asection *); extern bool _bfd_elf_link_keep_memory (struct bfd_link_info *); diff --git a/bfd/elf.c b/bfd/elf.c index cb47c499704..f66ac9289df 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -14388,10 +14388,12 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) return result; } -/* Mmap in section contents. */ +/* Mmap in section contents. If FINAL_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 final_link) { #ifdef USE_MMAP const struct elf_backend_data *bed = get_elf_backend_data (abfd); @@ -14415,16 +14417,41 @@ _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) if (sec->mmapped_p) abort (); sec->mmapped_p = 1; + + /* Never use the preallocated buffer if mmapp is used. */ + *buf = NULL; } } #endif - *buf = NULL; + /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is + true. If FINAL_LINK is false, *BUF is set to the preallocated + buffer if USE_MMAP is undefined and *BUF is set to NULL if + USE_MMAP is defined. */ + if (!final_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 the final link. */ + +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,3 +14491,25 @@ _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED, free (contents); } + +/* Munmap the full section contents for the final link. */ + +void +_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED) +{ +#ifdef USE_MMAP + if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL) + { + /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as + malloced, CONTENTS_ADDR is set to NULL. */ + /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ + if (munmap (elf_section_data (sec)->contents_addr, + elf_section_data (sec)->contents_size) != 0) + abort (); + sec->mmapped_p = 0; + sec->contents = NULL; + elf_section_data (sec)->contents_addr = NULL; + elf_section_data (sec)->contents_size = 0; + } +#endif +} diff --git a/bfd/elflink.c b/bfd/elflink.c index 4eb6ce5b3fa..386f64e9517 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11440,7 +11440,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; } @@ -11998,6 +11999,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); } return true; @@ -12436,13 +12440,17 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) /* Count up the number of relocations we will output for each output section, so that we know the sizes of the reloc sections. We also figure out some maximum sizes. */ - max_contents_size = 0; #ifdef USE_MMAP /* Mmap is used only if section size >= the minimum mmap section - size. max_external_reloc_size covers all relocation sections - smaller than the minimum mmap section size. */ + size. The initial max_contents_size value covers all sections + smaller than the minimum mmap section size. It may be increased + for compressed or linker created sections or sections whose + rawsize != size. max_external_reloc_size covers all relocation + sections smaller than the minimum mmap section size. */ + max_contents_size = _bfd_minimum_mmap_size; max_external_reloc_size = _bfd_minimum_mmap_size; #else + max_contents_size = 0; max_external_reloc_size = 0; #endif max_internal_reloc_count = 0; @@ -12478,10 +12486,19 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) if (sec->flags & SEC_MERGE) merged = true; - if (sec->rawsize > max_contents_size) - max_contents_size = sec->rawsize; - if (sec->size > max_contents_size) - max_contents_size = sec->size; +#ifdef USE_MMAP + /* Mmap is used only on non-compressed, non-linker created + sections whose rawsize == size. */ + if (sec->compress_status != COMPRESS_SECTION_NONE + || (sec->flags & SEC_LINKER_CREATED) != 0 + || sec->rawsize != sec->size) +#endif + { + if (sec->rawsize > max_contents_size) + max_contents_size = sec->rawsize; + if (sec->size > max_contents_size) + max_contents_size = sec->size; + } if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour && (sec->owner->flags & DYNAMIC) == 0)