From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id CC2D03858029 for ; Wed, 12 Jan 2022 02:28:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CC2D03858029 Received: by mail-pl1-x636.google.com with SMTP id u15so1930284ple.2 for ; Tue, 11 Jan 2022 18:28:04 -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=J8+SyvWjVbqxaDwnih6GriMZ21afLwb3gdi1ozNHd/M=; b=kXk1SEHv7KPTey/apuUYB/bJKQgLtTYhyJLz5tFCCqt+H5phtpAsmqBSNRweyJ0apj cdjtwBGQMMjlzR8LwQ2zqO8Kt7GkH3bVjM7iPt44N9NSuG+K5j+88JxyffmbLI8HO6v3 JpMCdN3QrJtIl2penBFq9YezwAffDGimfc3kZ6813RqCHAzsyvL/QymnGbstT8s52cVn DVeeh3Cy7UasJToQXRqdq13Sv44KZiGNtaLXtoGPygutE/5C9TkgaslRla9cACwPc3iT MFnqrulsfEgsQSZdsycl8zsp5LKK9qDeU3TpeVeTTp2ytFxqKAzXsoX49J9vUNud6IvJ ymhQ== X-Gm-Message-State: AOAM531Sl70eTjFapyFug2ADevwxBWDtl3MqMHg+ZZP73lZMfPStK6Ce ij0vQD3+w9N48X17duHpbMysY9lvupE= X-Google-Smtp-Source: ABdhPJzaMmgtuC4s/ShI0X4EgXH/QpLWswWs6hBfjIYRdL4d2hXWu2tG+rBcg9zuLgQdllOeyOsJlg== X-Received: by 2002:a17:90a:a013:: with SMTP id q19mr5612027pjp.10.1641954483958; Tue, 11 Jan 2022 18:28:03 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:54a9:1705:b55f:2d1]) by smtp.gmail.com with ESMTPSA id q11sm3310095pfk.212.2022.01.11.18.28.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jan 2022 18:28:03 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 972E811404B8; Wed, 12 Jan 2022 12:58:00 +1030 (ACDT) Date: Wed, 12 Jan 2022 12:58:00 +1030 From: Alan Modra To: "H.J. Lu" Cc: binutils@sourceware.org Subject: Re: [PATCH v3 2/9] elf: Extract _bfd_elf_process_reverse_copy Message-ID: References: <20220111140634.2250914-1-hjl.tools@gmail.com> <20220111140634.2250914-3-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220111140634.2250914-3-hjl.tools@gmail.com> X-Spam-Status: No, score=-3037.3 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.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: Wed, 12 Jan 2022 02:28:06 -0000 On Tue, Jan 11, 2022 at 06:06:27AM -0800, H.J. Lu via Binutils wrote: > Extract _bfd_elf_process_reverse_copy from elf_link_input_bfd so that > it can be called in check_relocs to set SEC_ELF_REVERSE_COPY before > elf_link_input_bfd is called. > > * elf-bfd.h (_bfd_elf_process_reverse_copy): New prototype. > * elflink.c (_bfd_elf_process_reverse_copy): New. Extracted > from elf_link_input_bfd. > (elf_link_input_bfd): Call _bfd_elf_process_reverse_copy. You are correct that the flag needs to be set earlier so that _bfd_elf_section_offset gives correct results when used earlier in the linking process. However, this is something that should be done once for all ELF targets rather than in multiple places depending on target, via a hook. It must be done during or after map_input_to_output_sections, and since your DT_RELR patchset makes decisions about relative relocations in ldemul_before_allocation, during or before that. But there isn't a convenient iteration over all sections in ldelf_before_allocation. So I'm inclined to set the flag during map_input_to_output_sections, even though it adds ELF specific code there. I'll commit the following after running tests over my normal list of targets. bfd/ * elflink.c (elf_link_input_bfd): Don't set SEC_ELF_REVERSE_COPY here. Move sanity checks to reverse copying code. ld/ * ldlang.c (lang_add_section): Set SEC_ELF_REVERSE_COPY for .ctors/.dtors in .init_array/.fini_array. diff --git a/bfd/elflink.c b/bfd/elflink.c index 059461b5725..f5e3fd53c5d 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11247,31 +11247,6 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) && o->reloc_count > 0) return false; - /* We need to reverse-copy input .ctors/.dtors sections if - they are placed in .init_array/.finit_array for output. */ - if (o->size > address_size - && ((startswith (o->name, ".ctors") - && strcmp (o->output_section->name, - ".init_array") == 0) - || (startswith (o->name, ".dtors") - && strcmp (o->output_section->name, - ".fini_array") == 0)) - && (o->name[6] == 0 || o->name[6] == '.')) - { - if (o->size * bed->s->int_rels_per_ext_rel - != o->reloc_count * address_size) - { - _bfd_error_handler - /* xgettext:c-format */ - (_("error: %pB: size of section %pA is not " - "multiple of address size"), - input_bfd, o); - bfd_set_error (bfd_error_bad_value); - return false; - } - o->flags |= SEC_ELF_REVERSE_COPY; - } - action_discarded = -1; if (!elf_section_ignore_discarded_relocs (o)) action_discarded = (*bed->action_discarded) (o); @@ -11756,9 +11731,24 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) offset *= bfd_octets_per_byte (output_bfd, o); - if ((o->flags & SEC_ELF_REVERSE_COPY)) + if ((o->flags & SEC_ELF_REVERSE_COPY) + && o->size > address_size) { /* Reverse-copy input section to output. */ + + if (o->reloc_count != 0 + && (o->size * bed->s->int_rels_per_ext_rel + != o->reloc_count * address_size)) + { + _bfd_error_handler + /* xgettext:c-format */ + (_("error: %pB: size of section %pA is not " + "multiple of address size"), + input_bfd, o); + bfd_set_error (bfd_error_bad_value); + return false; + } + do { todo -= address_size; diff --git a/ld/ldlang.c b/ld/ldlang.c index 9dbc8752f87..0af6c60bce5 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -2701,6 +2701,16 @@ lang_add_section (lang_statement_list_type *ptr, output->block_value = 128; } + /* When a .ctors section is placed in .init_array it must be copied + in reverse order. Similarly for .dtors. Set that up. */ + if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour + && ((startswith (section->name, ".ctors") + && strcmp (output->bfd_section->name, ".init_array") == 0) + || (startswith (section->name, ".dtors") + && strcmp (output->bfd_section->name, ".fini_array") == 0)) + && (section->name[6] == 0 || section->name[6] == '.')) + section->flags |= SEC_ELF_REVERSE_COPY; + if (section->alignment_power > output->bfd_section->alignment_power) output->bfd_section->alignment_power = section->alignment_power; -- Alan Modra Australia Development Lab, IBM