From patchwork Fri Jan 7 19:06:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 49725 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C2103857C4A for ; Fri, 7 Jan 2022 19:10:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C2103857C4A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1641582621; bh=AvRq8T+Y/X4lnD507eZM92Z+2OnMCJ7ez31YhAm4aiY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=HzQhGRvPQA7JXAKUgTRQA74/FGOcjJbGdbhnq7PPIjahXtdHFqP+P7PFYBt+qv+wn QjFvYq+mg66JRzTsfq32XQD27Q+LD/Ve53r9WaV2JAyuQSs4FJ2+QthEwBch+icZ0W ndWrLjijasSYcUZI31SNy8b0C2QGnV/6sSCEO5Fw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id C0E893858412; Fri, 7 Jan 2022 19:06:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C0E893858412 Received: by mail-pj1-x1029.google.com with SMTP id 59-20020a17090a09c100b001b34a13745eso5573149pjo.5; Fri, 07 Jan 2022 11:06:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AvRq8T+Y/X4lnD507eZM92Z+2OnMCJ7ez31YhAm4aiY=; b=G3vcL2gqAswif74tNyW4uXPR9R9eLKzam1AAj0EXMh43EaOVtTdtmN7RqiCZHAewBw CZwXzh2SoaV/PcrKwhUyuNbgUQuX/I+hRJkgUGsQoutoUu4D+oF204Mwxs+HX70Y1yUj gd5v3F0M7MnU4PFFA8+FUQ4d/K0BXa0Z33f0J6mSXKahmvnfTNKAazH0VLQDEuuhvHJ0 D7TNarmzPPzRdM6ef7VBKE/JN5O3YnDp1Ph2e6JmWVVO/L/PbDYa8PYL2slNlYVCj1zt 7kpxNtnKmgmfUY4WCcjNKYzspg+qFsJ68Ms0fCIaUM3RE+Db5YZijDfVeua8DbkVUnJN CBmg== X-Gm-Message-State: AOAM533K8fFV0a4i6D9AJkUg97cRS9tsV9hMORGpy7Xz/jBJHhaerF4L AXtGyzBlmTs5zRXI7qxYmpA= X-Google-Smtp-Source: ABdhPJx/8WvaxLQ2PbrItKKEmbyHFDKgG3+sbaBwQfzQw2LvFCwi5HFYSUWk4x5nEP1/TiFIlls9CQ== X-Received: by 2002:a17:902:dac4:b0:148:fde0:692a with SMTP id q4-20020a170902dac400b00148fde0692amr64672945plx.47.1641582393760; Fri, 07 Jan 2022 11:06:33 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id z3sm5476330pgc.45.2022.01.07.11.06.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jan 2022 11:06:33 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id F0336C0D75; Fri, 7 Jan 2022 11:06:31 -0800 (PST) To: binutils@sourceware.org Subject: [PATCH 03/10] elf: Extract _bfd_elf_process_reverse_copy Date: Fri, 7 Jan 2022 11:06:24 -0800 Message-Id: <20220107190631.309790-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220107190631.309790-1-hjl.tools@gmail.com> References: <20220107190631.309790-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3028.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: libc-alpha@sourceware.org, Nick Clifton , Alan Modra Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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. --- bfd/elf-bfd.h | 2 ++ bfd/elflink.c | 60 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 81f8fd47db7..2441b3c0cd7 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -2331,6 +2331,8 @@ extern const struct bfd_elf_special_section *_bfd_elf_get_special_section (const char *, const struct bfd_elf_special_section *, unsigned int); extern const struct bfd_elf_special_section *_bfd_elf_get_sec_type_attr (bfd *, asection *); +extern bool _bfd_elf_process_reverse_copy (asection *, unsigned int, + unsigned int); extern bool _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *, struct elf_link_hash_entry *); diff --git a/bfd/elflink.c b/bfd/elflink.c index 059461b5725..29ef9ddf8b9 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -10851,6 +10851,41 @@ _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) return kept; } +/* Set SEC_ELF_REVERSE_COPY on section S when we need to reverse-copy + input .ctors/.dtors sections if they are placed in .init_array or + .finit_array for output. ADDRESS_SIZE is address in bytes. + INT_RELS_PER_EXT_REL is the number of internal relocations to + allocate per external relocation entry. */ + +bool +_bfd_elf_process_reverse_copy (asection *s, unsigned int address_size, + unsigned int int_rels_per_ext_rel) +{ + if (s->size <= address_size + || (s->flags & SEC_ELF_REVERSE_COPY) != 0) + return true; + + if (((startswith (s->name, ".ctors") + && strcmp (s->output_section->name, ".init_array") == 0) + || (startswith (s->name, ".dtors") + && strcmp (s->output_section->name, ".fini_array") == 0)) + && (s->name[6] == 0 || s->name[6] == '.')) + { + if (s->size * int_rels_per_ext_rel + != s->reloc_count * address_size) + { + _bfd_error_handler + /* xgettext:c-format */ + (_("error: %pB: size of section %pA is not multiple of " + "address size"), s->owner, s); + bfd_set_error (bfd_error_bad_value); + return false; + } + s->flags |= SEC_ELF_REVERSE_COPY; + } + return true; +} + /* Link an input file into the linker output file. This function handles all the sections and relocations of the input file at once. This is so that we only have to read the local symbols once, and @@ -11249,28 +11284,9 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) /* 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; - } + if (!_bfd_elf_process_reverse_copy (o, address_size, + bed->s->int_rels_per_ext_rel)) + return false; action_discarded = -1; if (!elf_section_ignore_discarded_relocs (o))