From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id 393303858CD1 for ; Fri, 23 Jun 2023 11:40:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 393303858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1b5452b77b4so3929675ad.3 for ; Fri, 23 Jun 2023 04:40:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687520442; x=1690112442; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=JG0GEEvJplXz6zTJmzvPC9/UouSHgN20JHm0ckl3204=; b=B29VgxL9dr4zoD31UsT0afQuchgjd03LQ3PhqBy5BsQb2yC878WYUQhMjiTiL7w0ZZ g+mzfCYP1ZSEJ/+uE6xBuZ25K+XtaC7csnQeGCtQhXjCvdQbYG3v0xtRXwKqkTdXfw9w qqtMAxmTOIWv7wqqIdXdebSeQrs+pcmXNAiUaHDtVWUuBvtezFtzfuRF+sMnoE4eKs2F 7fDPtFZ31/KljQeSamIZsXI/0qCBStINNpsm3Y53ixc/HnqW7+Yqa9ou2qQdoYpGWBBe zdVkjnfquAFVXdse/hFyXPp1JBH0WPOWLLE7ivQMSrBg7CTx6ojoJW0dSFROwcwpk2+9 5PFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687520442; x=1690112442; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JG0GEEvJplXz6zTJmzvPC9/UouSHgN20JHm0ckl3204=; b=T84Oe9geOrH129QIAO9hS7K4aTRNM1DB7lpTTioNr/CUV7UJmbtS52jYDpyThhYpiI anc9AP71XhZtFWY8khaSziK6OUcu3Qwfce7wZUpa2hHTotZOQ1Z7WXmEENbNWxh0KsiQ 2bXWiiQpK3Wh1IKv76RuHM2wbLpkhAI80mEnVTp8nciAVoniWLsB9uhedx8ojVkmcKr8 uflhqafTEmeYd8qUU1c5gwhkwfsnHqDcOLv1PWeX+6A/GoQHNkHlgvX8IBptRQjFL/Jg 6dtMFDBjlKZN8n06RMs3TY7NdvH9DYfnWNI87pRnAleKqCOVMu2eJ9VQ1BdMPaOnhYKl 4GdA== X-Gm-Message-State: AC+VfDw/47lQQSojNO2iEgk4CydVmaFUdO6bZyaJlwaP4uefe3k7i9lX 8SSBjeyHJZXjB6MLzDyy0Xk/8xa63dg= X-Google-Smtp-Source: ACHHUZ5Ys4tBp67VYWyExp4QZQBz7OpNB4ylQhRW40FgQsjgM6+/svyu5GN9g2UqoSN/IfeHIK8hTg== X-Received: by 2002:a17:903:2284:b0:1b6:66f1:358f with SMTP id b4-20020a170903228400b001b666f1358fmr15223308plh.24.1687520441669; Fri, 23 Jun 2023 04:40:41 -0700 (PDT) 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 q15-20020a170902bd8f00b001a5fccab02dsm6994837pls.177.2023.06.23.04.40.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 04:40:41 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id C51EB1142EC8; Fri, 23 Jun 2023 21:10:38 +0930 (ACST) Date: Fri, 23 Jun 2023 21:10:38 +0930 From: Alan Modra To: binutils@sourceware.org Subject: [GOLD] PowerPC64 huge branch dynamic relocs Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3034.6 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: PowerPC64 gold and ld.bfd implement an indirect branch trampoline, used when the destination of a branch exceeds a bounce through another "b" instruction. When generating PIEs or shared libraries, the addresses need dynamic relocations. This was implemented in gold using a dedicated relocation section, but this means the relative relocations for these addresses are not sorted properly with other dynamic relative relocations: gold doesn't support merging relocation sections, then sorting. Instead we need to use a single .rela.dyn section. This is done by increasing the size of rela_dyn_ during do_relax to account for needed dynamic relocations, delaying adding the actual relocations until the end of relaxation once the layout has stabilised. * powerpc.cc (Target_powerpc): Add rela_dyn_size_; (update_current_size): New function. (Target_powerpc::do_relax): Capture the size of rela_dyn_ at the start of relaxation. Artifically increase its size during relaxation to account for needed indirect branches, and add those relocations at the end. (Output_data_brlt_powerpc::rel_, reset_brlt_sizes), (finalize_brlt_sizes, add_reloc, set_current_size): Delete. (Target_powerpc::make_brlt_section): Don't make reloc section. diff --git a/gold/powerpc.cc b/gold/powerpc.cc index 5838b49e341..96fef7b8dde 100644 --- a/gold/powerpc.cc +++ b/gold/powerpc.cc @@ -1763,6 +1763,8 @@ class Target_powerpc : public Sized_target Branches branch_info_; Tocsave_loc tocsave_loc_; + off_t rela_dyn_size_; + bool power10_relocs_; bool plt_thread_safe_; bool plt_localentry0_; @@ -3687,6 +3689,18 @@ Target_powerpc::Branch_info::make_stub( return ok; } +// Helper for do_relax, avoiding checks that size, address and offset +// are not set more than once. + +static inline void +update_current_size(Output_section_data_build* od, off_t cur_size) +{ + od->reset_address_and_file_offset(); + od->set_current_data_size(cur_size); + od->finalize_data_size(); + od->output_section()->set_section_offsets_need_adjustment(); +} + // Relaxation hook. This is where we do stub generation. template @@ -3752,10 +3766,11 @@ Target_powerpc::do_relax(int pass, } } this->plt_thread_safe_ = thread_safe; - } - if (pass == 1) - { + if (parameters->options().output_is_position_independent()) + this->rela_dyn_size_ + = this->rela_dyn_section(layout)->current_data_size(); + this->stub_group_size_ = parameters->options().stub_group_size(); bool no_size_errors = true; if (this->stub_group_size_ == 1) @@ -3868,7 +3883,15 @@ Target_powerpc::do_relax(int pass, if (size == 64 && num_huge_branches != 0) this->make_brlt_section(layout); if (size == 64 && again) - this->brlt_section_->set_current_size(num_huge_branches); + { + update_current_size(this->brlt_section_, num_huge_branches * 16); + if (parameters->options().output_is_position_independent()) + { + const unsigned int reloc_size = elfcpp::Elf_sizes::rela_size; + off_t cur = this->rela_dyn_size_ + num_huge_branches * reloc_size; + update_current_size(this->rela_dyn_, cur); + } + } for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin(); p != this->stub_tables_.rend(); @@ -3937,15 +3960,21 @@ Target_powerpc::do_relax(int pass, && parameters->options().output_is_position_independent()) { // Fill in the BRLT relocs. - this->brlt_section_->reset_brlt_sizes(); + this->rela_dyn_->reset_data_size(); + this->rela_dyn_->set_current_data_size(this->rela_dyn_size_); for (typename Branch_lookup_table::const_iterator p = this->branch_lookup_table_.begin(); p != this->branch_lookup_table_.end(); ++p) { - this->brlt_section_->add_reloc(p->first, p->second); + this->rela_dyn_->add_relative(elfcpp::R_POWERPC_RELATIVE, + this->brlt_section_, p->second, + p->first); } - this->brlt_section_->finalize_brlt_sizes(); + this->rela_dyn_->finalize_data_size(); + const unsigned int reloc_size = elfcpp::Elf_sizes::rela_size; + gold_assert(this->rela_dyn_->data_size() + == this->rela_dyn_size_ + num_huge_branches * reloc_size); } if (!again @@ -4554,52 +4583,11 @@ class Output_data_brlt_powerpc : public Output_section_data_build typedef Output_data_reloc Reloc_section; - Output_data_brlt_powerpc(Target_powerpc* targ, - Reloc_section* brlt_rel) + Output_data_brlt_powerpc(Target_powerpc* targ) : Output_section_data_build(size == 32 ? 4 : 8), - rel_(brlt_rel), targ_(targ) { } - void - reset_brlt_sizes() - { - this->reset_data_size(); - this->rel_->reset_data_size(); - } - - void - finalize_brlt_sizes() - { - this->finalize_data_size(); - this->rel_->finalize_data_size(); - } - - // Add a reloc for an entry in the BRLT. - void - add_reloc(Address to, unsigned int off) - { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); } - - // Update section and reloc section size. - void - set_current_size(unsigned int num_branches) - { - this->reset_address_and_file_offset(); - this->set_current_data_size(num_branches * 16); - this->finalize_data_size(); - Output_section* os = this->output_section(); - os->set_section_offsets_need_adjustment(); - if (this->rel_ != NULL) - { - const unsigned int reloc_size = elfcpp::Elf_sizes::rela_size; - this->rel_->reset_address_and_file_offset(); - this->rel_->set_current_data_size(num_branches * reloc_size); - this->rel_->finalize_data_size(); - Output_section* os = this->rel_->output_section(); - os->set_section_offsets_need_adjustment(); - } - } - protected: void do_adjust_output_section(Output_section* os) @@ -4617,8 +4605,6 @@ class Output_data_brlt_powerpc : public Output_section_data_build void do_write(Output_file*); - // The reloc section. - Reloc_section* rel_; Target_powerpc* targ_; }; @@ -4630,24 +4616,15 @@ Target_powerpc::make_brlt_section(Layout* layout) { if (size == 64 && this->brlt_section_ == NULL) { - Reloc_section* brlt_rel = NULL; bool is_pic = parameters->options().output_is_position_independent(); if (is_pic) { // When PIC we can't fill in .branch_lt but must initialise at // runtime via dynamic relocations. this->rela_dyn_section(layout); - // FIXME: This reloc section won't have its relative relocs - // sorted properly among the other relative relocs in rela_dyn_ - // but it must be a separate section due to needing to call - // reset_data_size(). - brlt_rel = new Reloc_section(false); - if (this->rela_dyn_->output_section()) - this->rela_dyn_->output_section() - ->add_output_section_data(brlt_rel); } this->brlt_section_ - = new Output_data_brlt_powerpc(this, brlt_rel); + = new Output_data_brlt_powerpc(this); if (this->plt_ && is_pic && this->plt_->output_section()) this->plt_->output_section() ->add_output_section_data(this->brlt_section_); -- Alan Modra Australia Development Lab, IBM