From patchwork Thu Mar 2 12:01:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 63387 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp4193786wrd; Thu, 2 Mar 2023 04:01:39 -0800 (PST) X-Google-Smtp-Source: AK7set+GKduSqNB4RLFKJkEOFtOVFzy9IWExv+TQX8MGigCkXuPj75m8YfWbxaEaLaPLiwnvoTVi X-Received: by 2002:a17:906:34d9:b0:8b2:7150:dcff with SMTP id h25-20020a17090634d900b008b27150dcffmr9966257ejb.23.1677758499014; Thu, 02 Mar 2023 04:01:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677758499; cv=none; d=google.com; s=arc-20160816; b=prRoWuxIuHOdUJwzOOr4X6YXAhShkpKVMwiJC4hXPsG1uoMC2dsvLBMKcdvOVjzkWJ X0w04xbyEBkqdyKeDci2NCPs1i3iscQj6JSKyyQpsaTjCUQVu+n1GbvAK2rj23wp/ZW7 V5z7SONXJwVP/4b5dTxorSNRYt933nt61ZWSplMhqTIlN+8ukHTpbJfe0m9Jh/sKF9jS Wtw2ynofCQsH6//9mQZKVTgI/Fu9za2J/jbImiFjByFYX7SHJifDziFsgjsUWPm/r/50 pLwqtrOxi9a6C97zbAV9Um+amuW5HU8mteuP+XgpD1KlfnDUYd5K2hayIsgpiuoWk7sN cnvw== 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=lQBb0YWFcrH+EsqHqW1U6aDoTeZLRjawfwMhNbjzR1g=; b=pXdHnb23Uo2JLSaJzmiZyyk5FUWH06MKUzNYmUbTsxi9270CNGODOY+tMQMN5NtL5s 4pXqruzl/dhDEn9r/xdPO7K++nKNK9HuNyZQa8fOOZhfaNbI6CCQoC6Xwndht54AXslg VtTPILi/ctDuy1b4WeK38Dp6pBCAjjbidtrITNMddfpcGiKue+HAILvSTeF3zzWIJZAa dObJrBVkSjEvj/bujwDuV1JVpY2eseKEpkY1Oenwe2fzP4wjqwRsepqEYfUjgRN8msKr hhrEkqxw1i2sulpKHnaaKBXQHi0nhoP5mde6LmY5lvCYzm77IPl0/iugjbm+0b4H+YYX VwUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="mOc/OQA+"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a16-20020a1709065f9000b008b17569b4d9si4836624eju.361.2023.03.02.04.01.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 04:01:38 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="mOc/OQA+"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 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 0AD3B3858C54 for ; Thu, 2 Mar 2023 12:01:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0AD3B3858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677758498; bh=lQBb0YWFcrH+EsqHqW1U6aDoTeZLRjawfwMhNbjzR1g=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=mOc/OQA+JoXa0asueNVcxtcICA4NAh2Y2W+PUs+BOf3s5I53khHAGYM6B5i7XKuqn 7JLR2aJjNC75x0uVTxBW9ttqQ1pwGfoPv+EBeQ2c7HsdeS9gD6/TY270JM8UUkrMug S8026/sE76EP8U1WMeNA+xW1FaCmz0WwNkINAtq8= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id D41AD3858D33 for ; Thu, 2 Mar 2023 12:01:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D41AD3858D33 Received: by mail-pj1-x1032.google.com with SMTP id m3-20020a17090ade0300b00229eec90a7fso3467395pjv.0 for ; Thu, 02 Mar 2023 04:01:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677758488; 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=lQBb0YWFcrH+EsqHqW1U6aDoTeZLRjawfwMhNbjzR1g=; b=bc6atZykX2Wb2BJsUk1XdMOrUwMgVOLJbKieAnIhqDosAJeJGpbu5ThDri3MfYMwXF 3FvWbAezb0sDDzrn5YuWd6QTWZUEZQefavM/xGMIMNTEM8rfddUzTSbFbdgUIktISUCz NqmQLjhzR+8TBYeHyCOji3lzJR+iyHjHwN4KPtvTiQFUKuq8+Ovgq1l1n65U46imF15+ XAJGDa//eaSr8W8+4H5UGWTvT2h/NhMoPJyxjj62rKmZ7p6ZdgZWdy5zDOenxmDe48zz /HLI5i2aPy7qNximre3kKAGvWlqIRgB2xKA7yzwy40ox//GHllYurOrRoJpdB5LJOa5S UeBw== X-Gm-Message-State: AO0yUKWMExrhcuP2hlbn/XHBwqhYkKLlIW1Vc06ATqa99rVJwLTrNOQr k8iX2Ooc5KO/MmH+pe2rReIai3yDb7I= X-Received: by 2002:a17:902:ea0e:b0:19a:9580:74c with SMTP id s14-20020a170902ea0e00b0019a9580074cmr11392183plg.7.1677758488615; Thu, 02 Mar 2023 04:01:28 -0800 (PST) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id j24-20020a170902759800b0019a60b7cc0esm10155163pll.248.2023.03.02.04.01.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 04:01:28 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id BB0761142C90; Thu, 2 Mar 2023 22:31:25 +1030 (ACDT) Date: Thu, 2 Mar 2023 22:31:25 +1030 To: binutils@sourceware.org Subject: Don't write zeros to a gap in the output file Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.4 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?1759257295795774426?= X-GMAIL-MSGID: =?utf-8?q?1759257295795774426?= Writing out zeros is counterproductive if a file system supports sparse files. A very large gap need not take much actual disk space, but it usually will if zeros are written. memory_bseek also supports not writing out zeros in a gap. * elf.c (write_zeros): Delete. (assign_file_positions_for_load_sections): Don't call write_zeros. Comment. diff --git a/bfd/elf.c b/bfd/elf.c index 37f331b98cd..409be7068ec 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -5402,22 +5402,6 @@ print_segment_map (const struct elf_segment_map *m) fflush (stderr); } -static bool -write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) -{ - void *buf; - bool ret; - - if (bfd_seek (abfd, pos, SEEK_SET) != 0) - return false; - buf = bfd_zmalloc (len); - if (buf == NULL) - return false; - ret = bfd_bwrite (buf, len, abfd) == len; - free (buf); - return ret; -} - /* Assign file positions to the sections based on the mapping from sections to segments. This function also sets up some fields in the file header. */ @@ -5866,11 +5850,13 @@ assign_file_positions_for_load_sections (bfd *abfd, if (p->p_filesz + adjust < p->p_memsz) { /* We have a PROGBITS section following NOBITS ones. - Allocate file space for the NOBITS section(s) and - zero it. */ + Allocate file space for the NOBITS section(s). + We don't need to write out the zeros, posix + fseek past the end of data already written + followed by a write at that location is + guaranteed to result in zeros being read + from the gap. */ adjust = p->p_memsz - p->p_filesz; - if (!write_zeros (abfd, off, adjust)) - return false; } } /* We only adjust sh_offset in SHT_NOBITS sections