From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id DB57F3857B86 for ; Thu, 14 Mar 2024 13:25:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB57F3857B86 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 DB57F3857B86 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710422762; cv=none; b=TqDQhyZDlzj9dyeYaGmVaTCeQtRDSmx/nJb8w1lN7VgJQfZ7Y6WGrvlIWmGgDpNHMa1614AIgm/C2smjh/XOBQjdy/d7jKDz1I2gdKVziDHgjqNIZRCKH8OU7UJdoy5c2F7xf8fUoQLPSU34fB65NiTLqGXFuHxQ6Odei8ecAs4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710422762; c=relaxed/simple; bh=wxsx8141irqhwQL0tJ6p9JSgUDE25n0xnfyWwgY8UTs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=apo0Zr9ooaVEhedRzTQfONs6Bq5MWlu2emWdNNX85i1Bj9ppkJSFO9NTBj6bFEmyRIRX3Zr6b7p1fWNvR0lQDSf3Vr/+Pw6DBxJUdPijIvX0ovc6pO6fgIUOnNmLv4d/SIrSx/XJjLX6h91F6v1Y9+7Bt8JSMXGXcvJVDn4vCPA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-29c6a917a8dso833125a91.1 for ; Thu, 14 Mar 2024 06:25:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710422758; x=1711027558; 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=jaO/9JcwZ92NIYKBijDRBHsx/kkgcXiCjhejofhuJO4=; b=BBn1izHOrL5+mgzpk3QK51k3ay8snMC8lVJYl6kIGdt7iRsaPTAg65sJJpsUuoxUtV Q8/hyKynxMBig21QNTt8d9KEpo3Ktu0Qe73cV4Ni1Nvv2z5YWTKyVTWINskktfE7Tjm7 e/nY+6kIx5z4U26LctLntoliW5+j4d4xxBOH8UXS23v5ke/6/i2xZw6QXiUNDJ3Pe8u4 UTRE+7cQgyKbD07iVWvWlAfi/O/capP9/enK9qHtKVwqBYJRR/kw0wi3ci7ZyQ+HlKM/ LjIM0CtDIseYuKQfU9D3k3zsHejXrt5q1w0lmyAn8OIz3LU5JanA04kpH69RF5Tu7rj6 O4tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710422758; x=1711027558; 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=jaO/9JcwZ92NIYKBijDRBHsx/kkgcXiCjhejofhuJO4=; b=kIvQ69OZSVnqILvn3OWRSpGtJ2M9syRcXwMYY2e+6Ivi0T8YqndfKpalnfzW5G4Vze FjSlOodgfm1qGAcu28UeBNt+RA+jSs28uWpQafYI7zJTHSckmF9/T5lFXv1hqIx8MS16 CHoPy4+uzB8UE6hrQloOTXfqMwaTjor4FgLMYQYp2guG6WXb0ZolaDxOWQWUiAOO2ZCy M0Nh9Mhgb+x5IQLyJKecm+6wCjt9sTfGqTpW9KVN6zMNqkHFnfXCQMQAHF44q+eHoLyr R5qYOqJufcTywb1XvZQTWgEKGtM3xxXyA2SKQGeUYOloqPEyV8igiO9Yx+NLWmOzM0EQ dCPQ== X-Gm-Message-State: AOJu0YzK24JKr0PK35U3IbHQCFLICOTahzPc0LRjnw8pkoIP9bwxm8kF wTqABqvj/giQ4m+Io8VTpPSeU5hbSncUiL4e76wnbr9VzCacc9M7 X-Google-Smtp-Source: AGHT+IFEl9T1B33gVlSz6C0zaGslZ4HxomjL4wgNS03keKiF9BUAwSSHwRMnjcYMIV1ifsnTxpTYXw== X-Received: by 2002:a17:90b:c12:b0:29b:b736:71b8 with SMTP id bp18-20020a17090b0c1200b0029bb73671b8mr1646736pjb.28.1710422757750; Thu, 14 Mar 2024 06:25:57 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.58.89.72]) by smtp.gmail.com with ESMTPSA id p7-20020a17090a348700b0029b77fbeb7fsm934242pjb.16.2024.03.14.06.25.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 06:25:57 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id ED39E74071D; Thu, 14 Mar 2024 06:25:52 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Cc: goldstein.w.n@gmail.com, sam@gentoo.org, amodra@gmail.com Subject: [PATCH v10 6/6] elf: Add _bfd_elf_link_m[un]map_section_contents Date: Thu, 14 Mar 2024 06:25:52 -0700 Message-ID: <20240314132552.83916-7-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240314132552.83916-1-hjl.tools@gmail.com> References: <20240314132552.83916-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3019.4 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. * 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 | 26 +++++++++++++++++------ 3 files changed, 77 insertions(+), 10 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 40440445be1..2ed550d3041 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 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); @@ -14443,16 +14445,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 @@ -14492,3 +14519,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 04c23b3c7f3..28d587ceeed 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); } return true; @@ -12459,14 +12463,15 @@ 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. The initial 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_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; @@ -12502,10 +12507,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) -- 2.44.0