From patchwork Sat Jan 8 18:38:34 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: 49758 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 86DC1386F013 for ; Sat, 8 Jan 2022 18:49:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 86DC1386F013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1641667764; bh=/vZ6gCMPu8UV8jYvh4DQxd1Ke9f1uLbhrhjIl8JsiIg=; 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=cydiY/UynKD1Xcjcfq9W7aSCLzUBjeM74iTf2fPeALJAUzUyHzNoq8mUgH36/Y4eK tBkp+dFJvG09k5ed763/au0AEoky4gHjxAZrcM8w/zxUvexnprZhK7SEUHNa43Z486 ofKUbsr3WfQ99qzFnCBNr/7/8caiImkKurmNEslo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by sourceware.org (Postfix) with ESMTPS id 216F3386EC25; Sat, 8 Jan 2022 18:38:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 216F3386EC25 Received: by mail-pj1-x102b.google.com with SMTP id oa15so8234199pjb.4; Sat, 08 Jan 2022 10:38:43 -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=/vZ6gCMPu8UV8jYvh4DQxd1Ke9f1uLbhrhjIl8JsiIg=; b=qysgd3bwj0e7WMdlKoIwK19txSmkg/yS6e7kmLOdDcnqHpqftZ2lN/7UyErhFwKBC5 ivlZDd5NTNs7VA6ExxwPg62sWMT4AmZA6CSBzGGdFmVsMmsxvmhiSLPTvjDUuhkYOLrB MYtifLpwWQ4D0ppPXrWbo0i/OlIF4KzPkjQ11S7a82j0TtWva6EafcBag9qjfticfK3o Dy8za8BJRFiCqAzvj2CQJ2vHsp8y31ji7UCsqy/sIYgJEmQSwjy0YVOZr25psfmNat/v zxvI3SqKkwQaxkDNlj3wXndDhmEuJYSnN8rnOXDW2QF33D3K1GKL62txILnR1gm9Bayu dhLg== X-Gm-Message-State: AOAM532F2Qrj11QM00F3966uocu85RBY9cMZpRHAPRZjz5uObOZJP09O pT4Z9Q2RyV2PXA4mJV2wwgU= X-Google-Smtp-Source: ABdhPJx3rbMWwz8ozGRw4gD1/FnXvOH6P85RloPV1VWTTl1FXjKC2WUTNRKu4o6ZIjKh3x+/No8JJQ== X-Received: by 2002:a63:8f4a:: with SMTP id r10mr8775654pgn.414.1641667122257; Sat, 08 Jan 2022 10:38:42 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id k62sm2549417pja.23.2022.01.08.10.38.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jan 2022 10:38:41 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 2BAE0C0EDF; Sat, 8 Jan 2022 10:38:39 -0800 (PST) To: binutils@sourceware.org Subject: [PATCH v2 5/9] elf: Add size_relative_relocs and finish_relative_relocs Date: Sat, 8 Jan 2022 10:38:34 -0800 Message-Id: <20220108183838.1920397-6-hjl.tools@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220108183838.1920397-1-hjl.tools@gmail.com> References: <20220108183838.1920397-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3028.8 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" On some targets, the DT_RELR section size can be computed only after all symbols addresses can be determined. Set the preliminary DT_RELR section size before mapping sections to segments and set the final DT_RELR section size after regular symbol processing is done. * elf-bfd.h (elf_backend_data): Add size_relative_relocs and finish_relative_relocs. * elf.c (_bfd_elf_map_sections_to_segments): Call size_relative_relocs if DT_RELR is enabled. * elflink.c (bfd_elf_final_link): Call finish_relative_relocs after regular symbol processing is finished if DT_RELR is enabled. * elfxx-target.h (elf_backend_size_relative_relocs): New. (elf_backend_finish_relative_relocs): Likewise. (elfNN_bed): Add elf_backend_size_relative_relocs and elf_backend_finish_relative_relocs. --- bfd/elf-bfd.h | 10 ++++++++++ bfd/elf.c | 14 ++++++++++++-- bfd/elflink.c | 8 ++++++++ bfd/elfxx-target.h | 8 ++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 4e73d79ee77..c006008ab7e 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1105,6 +1105,16 @@ struct elf_backend_data (bfd *abfd, struct bfd_link_info *info, asection *o, const Elf_Internal_Rela *relocs); + /* The SIZE_RELATIVE_RELOCS function is called to size relative + relocations when mappig sections to segments. */ + bool (*size_relative_relocs) + (struct bfd_link_info *info, bool *need_layout); + + /* The FINISH_RELATIVE_RELOCS function is called to finish relative + relocations in bfd_elf_final_link. */ + bool (*finish_relative_relocs) + (struct bfd_link_info *info); + /* The CHECK_DIRECTIVES function is called once per input file by the add_symbols phase of the ELF backend linker. The function must inspect the bfd and create any additional symbols according diff --git a/bfd/elf.c b/bfd/elf.c index 8b866b63e18..14c2c7ba734 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -4615,7 +4615,7 @@ elf_modify_segment_map (bfd *abfd, bool _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info, - bool *need_layout ATTRIBUTE_UNUSED) + bool *need_layout) { unsigned int count; struct elf_segment_map *m; @@ -4626,7 +4626,17 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, no_user_phdrs = elf_seg_map (abfd) == NULL; if (info != NULL) - info->user_phdrs = !no_user_phdrs; + { + info->user_phdrs = !no_user_phdrs; + + /* Size the relative relocations if DT_RELR is enabled. */ + if (info->enable_dt_relr + && need_layout != NULL + && bed->size_relative_relocs + && !bed->size_relative_relocs (info, need_layout)) + info->callbacks->einfo + (_("%F%P: failed to size relative relocations\n")); + } if (no_user_phdrs && bfd_count_sections (abfd) != 0) { diff --git a/bfd/elflink.c b/bfd/elflink.c index d51b00b6c10..31b13f5df7a 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -12625,6 +12625,14 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) if (!_bfd_elf_fixup_eh_frame_hdr (info)) return false; + /* Finish relative relocations here after regular symbol processing + is finished if DT_RELR is enabled. */ + if (info->enable_dt_relr + && bed->finish_relative_relocs + && !bed->finish_relative_relocs (info)) + info->callbacks->einfo + (_("%F%P: %pB: failed to finish relative relocations\n"), abfd); + /* Since ELF permits relocations to be against local symbols, we must have the local symbols available when we do the relocations. Since we would rather only read the local symbols once, and we diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 360b056ff58..e31985ef777 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -478,6 +478,12 @@ #ifndef elf_backend_check_relocs #define elf_backend_check_relocs 0 #endif +#ifndef elf_backend_size_relative_relocs +#define elf_backend_size_relative_relocs 0 +#endif +#ifndef elf_backend_finish_relative_relocs +#define elf_backend_finish_relative_relocs 0 +#endif #ifndef elf_backend_check_directives #define elf_backend_check_directives 0 #endif @@ -842,6 +848,8 @@ static const struct elf_backend_data elfNN_bed = elf_backend_omit_section_dynsym, elf_backend_relocs_compatible, elf_backend_check_relocs, + elf_backend_size_relative_relocs, + elf_backend_finish_relative_relocs, elf_backend_check_directives, elf_backend_notice_as_needed, elf_backend_adjust_dynamic_symbol,