From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by sourceware.org (Postfix) with ESMTPS id 397563858D28 for ; Sun, 30 Jan 2022 01:08:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 397563858D28 Received: by mail-qv1-xf29.google.com with SMTP id o9so9385071qvy.13 for ; Sat, 29 Jan 2022 17:08:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=PlZCQz/mHlUNXJdUIbO9lQCO78oHthMehSjHZVGUfss=; b=LL9RiM21hnGYJZztL5uDbi+xXtplNzhULF5pqmnDj/600QvmtKYT1h5b1l1tT0A8S8 MfhmhTjS4GYLxmReI2Yd0lKmbp2hsYXEyQV9yvXd53xLkO3Usk9sGwKyTnPTzn7qGFzs 3epKeHPiNxivzc+yWik/uE1E5CmZ3FvB65twzxNeyU8im1d3t4reL+9Mo2gMrV57IWJy QwfkVyQpafo8jKqh0c8o8GumMmSinzyOIou2a2WPUM/Xje85Z6Dm2Dq5EcjW1NCky9Nl ExB0BEJKSk4OwSJcvVCsI1oHNpI6ZAzqxF+YHLrj5sM/xLQSuScjLAVErz/3/0Lq/7Py J1iQ== X-Gm-Message-State: AOAM531guSrn3z6HRX5zT3LaJwwypBGhieruoKOJXAnlCUhmod2naoh4 D2d/W2HgEUIsdOWLBJciEA== X-Google-Smtp-Source: ABdhPJyloV4tHTcjusxDARH2N50YO3WXYja7Ea+7p+76dzxFAeKWxIw4FhdZ31XEof4Sn7aKKbNODQ== X-Received: by 2002:a05:6214:c44:: with SMTP id r4mr12813122qvj.96.1643504927519; Sat, 29 Jan 2022 17:08:47 -0800 (PST) Received: from bytedance (ec2-52-52-7-82.us-west-1.compute.amazonaws.com. [52.52.7.82]) by smtp.gmail.com with ESMTPSA id x18sm6006387qtw.93.2022.01.29.17.08.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jan 2022 17:08:46 -0800 (PST) Date: Sat, 29 Jan 2022 17:08:44 -0800 From: Peilin Ye To: Alan Modra Cc: binutils@sourceware.org, Cong Wang , Peilin Ye , Nick Clifton Subject: Re: [RFC PATCH] bfd: Handle objcopy --only-keep-debug in _bfd_elf_init_private_section_data() Message-ID: <20220130010844.GA69456@bytedance> References: <20220128035817.65910-1-yepeilin.cs@gmail.com> <20220129091540.GA68164@bytedance> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220129091540.GA68164@bytedance> X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Sun, 30 Jan 2022 01:08:50 -0000 On Sat, Jan 29, 2022 at 01:15:40AM -0800, Peilin Ye wrote: > It seems that --only-keep-debug is broken for Linux kernel .ko modules, > but it works just fine on tmpdir/striprog (built from testprog.c) used > by this testsuite... I figured this out; it's because --only-keep-debug is only broken for ELF relocatables. For dynamically linked and statically linked ELF executables, their non-debug output sections' type is changed 3 times: 1. In setup_section() "make_nobits", set to SHT_NOBITS; 2. Then in _bfd_elf_init_private_section_data(), restored back to input section's type, as we already discussed; 3. Finally in assign_file_positions_for_load_sections(), they are set once again to SHT_NOBITS: for (i = 0; i < m->count; i++) if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) /* If we aren't making room for this section, then it must be SHT_NOBITS regardless of what we've set via struct bfd_elf_special_section. */ elf_section_type (m->sections[i]) = SHT_NOBITS; ^^^^^^^^^^ For a complete call graph: copy_object() [...] setup_section() /* 1. set to SHT_NOBITS */ bfd_copy_private_section_data() _bfd_elf_copy_private_section_data() _bfd_elf_init_private_section_data() /* 2. restore to ISEC's type */ [...] copy_section() bfd_set_section_contents() _bfd_elf_set_section_contents() _bfd_elf_compute_section_file_positions() assign_file_positions_except_relocs() if (EXEC_P or DYNAMIC) /* ELF executables? e.g. tmpdir/striprog */ assign_file_positions_for_load_sections() /* 3. set again to SHT_NOBITS */ else /* ELF relocatables? e.g. Linux modules */ Frankly, I don't semantically understand what this code is trying to do here yet. Since an ELF relocatable is neither EXEC_P nor DYNAMIC, assign_file_positions_for_load_sections() is not called, therefore its non-debug OSECs remain the old (ISEC's) type. In conclusion, I think currently --only-keep-debug is only broken for ELF relocatables, although I'm not 100% sure. I will add this information in v3's commit message, and come up with a better testsuite (probably test this on a .o file or something). Thanks, Peilin Ye