From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 3D0173857BBC for ; Wed, 13 Mar 2024 15:52:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D0173857BBC 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 3D0173857BBC Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710345156; cv=none; b=mWG+GO+ZE0e41EOCMiVlPGp99Q/bKPfctHu83r5wrhQOdmnXLGDm2nO71r5c55vCpifPw7PN1gVlDgJp6UdpVF/Vz8SfnGnhDcrVsOUZXS2LDOgafWuKSnMeeTOtBuca7PgLfSzkxqTSrdhT3JL9N3n3D7L0/lUpGDIh0ZENyGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710345156; c=relaxed/simple; bh=JR+6xDfDVwkPugfTfL8aqCQ3xFE7vlEGvXsqzyT8/7g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=FxA9omytr+6amoICoLtqSUEybpw2qfUy6qw7Ewg1zMyijMSm1cw4Kmp/8xrfUzq8tSTAZMKiiyyuqO0aaYuKnTE2tFnoYp8jPXXk0y/rzzuLmsPvN/e6JPoYTnSeCBtry8u27PdGzNZtarRO228KiKqtHg12DbqLtfKwzrWX3wI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6e46dcd8feaso22779b3a.2 for ; Wed, 13 Mar 2024 08:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710345153; x=1710949953; 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=EdcfzkRqSDy4aSfBrGz5a+wHHqlmJtgU74R8LN81pqza4vu6lWW0EwL4fNkwwIiN09 /4AzHMa6cpVhG0egnx50N1sAGvh/Yaq4ur1aIINrhEBiDcDVjAAVlJw7VZvZ+71XdGE9 wDcj3q0wGAlz5vA+E9WcUYbzb8xHKRxTKbQqWXYhdsgtTEUh366PFgckhzEyd1hkvlpV DJ4j4hvD5GErrj3wRmITW3jhYma1XPl2q0caAQatABSpZNR7gT078VAV2eUrehYTa3L/ bZL9fpyaEgOkD7hhP5dwZC7ncdpYIe9v519akhJK+6MQVmySdmObgTvYrYj/qW7onbF5 u55g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710345153; x=1710949953; 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=KtY/A7ZuWpGc8cX5eOWfg9gldGzHU699mNXw9tYJfliPSLe2vaKMaUX85f/ZUKiQUp K2weflof0crQ4LBm+1xgmuivK4GFV/9dnnqW9RFfPgbBw4jyIA5Ji6e5KQdXEhLIxczd bKe8KZ/PTOIi/YY87orFMrobDaPI6cUQYbHAhiaVEMG3D1om2b5vgARO4d3hgwhP+7Uj Kd7hYCvQVqPGI0bcCReUMHWh1gXsRN5avbs8o//V+5VqNzHEovsuJUar47p2/n4vNnO5 7Hslg/GSH2nX5IM/D4HYlfbvYzzYY02cVGeTBxzC7I4Fl+Z+RR8LV+VPlqIyIX+tPvwP eO+w== X-Gm-Message-State: AOJu0Yxx0M783imhbq9BZzOgdW13i1jJvOvL72yAqtai5nso7HJw+TJX 0WMPqTcrmYtoRYAM37ZvkFgyAbre6ZcZAiSVkTn+NUJG34sGgkyQ X-Google-Smtp-Source: AGHT+IEaJvQBvTBcGHu9Hzls826tVAuj3y/jLXVi4XFt3RN838CNaDEgMAl5N+4iue2mvIro2Y5X9g== X-Received: by 2002:a05:6a20:429a:b0:1a1:4a4c:9f5e with SMTP id o26-20020a056a20429a00b001a14a4c9f5emr6020956pzj.49.1710345152953; Wed, 13 Mar 2024 08:52:32 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.58.89.72]) by smtp.gmail.com with ESMTPSA id b27-20020aa78edb000000b006e6b7124b33sm1565866pfr.209.2024.03.13.08.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 08:52:32 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 23A7974071D; Wed, 13 Mar 2024 08:52:28 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Cc: goldstein.w.n@gmail.com, sam@gentoo.org, amodra@gmail.com Subject: [PATCH v8 6/6] elf: Add _bfd_elf_link_m[un]map_section_contents Date: Wed, 13 Mar 2024 08:52:27 -0700 Message-ID: <20240313155227.513873-7-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240313155227.513873-1-hjl.tools@gmail.com> References: <20240313155227.513873-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