From patchwork Tue Dec 15 09:16:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 41391 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 ADA6738618C9; Tue, 15 Dec 2020 09:17:13 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by sourceware.org (Postfix) with ESMTPS id 4F29E38618B6 for ; Tue, 15 Dec 2020 09:17:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4F29E38618B6 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vincent.chen@sifive.com Received: by mail-pf1-x443.google.com with SMTP id t22so4732944pfl.3 for ; Tue, 15 Dec 2020 01:17:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tmphHp54vGV4mg5PxnQWZBYadK6tvJwd6MBeW6YHQN4=; b=OJms05jHLMqshozM293OlW8HjvkdkjhvYgvJGXzQClhTxKFkghg6kf5S477kke7l1x i3KGJAP53z5+J7DxUTjNcxrDm9Jkxvn9GBa5PgMedrSR9Je+6RX4i170Df/AKfAODiaY /ADoQ4HuwNgaa/z3sEDz33myETQ7wv695dkSJS3fhH/QHzFc8Y7HcYlncPYW2cCBZvWo maKV+ZwNlrZw2IXWGaIUhHl9Zm5KeoxKkVeptvy1GcLqUG9a1dNUFcWX0fXlGNqhfAU8 2scpKNYFTpoTpGo+T3wAJ7Tv/cHRhMON5ss75GOqyebzJ8+KE+LeY54+rFpoLY1mCKfA 3K/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tmphHp54vGV4mg5PxnQWZBYadK6tvJwd6MBeW6YHQN4=; b=aSHp6Q1qwdVT1Bi4uypeRq3+BRSnDbFHU9ty+tprksJMoA4D4fCyEeNpEXKCNv3N1O 2wk7b/ZrAIfPkQJwnznVTkTCwNt3r9FZfdazYEQH/uwLZYUI9nLflE+GvkZpeAX62vBT QSwKnDe2DlzAsF874wQhLTJ1Q5DGENmzH9//YLsA7k73Rcc3RhGrTS4Re4CsajD9gZ4X JE1ONm/lHbAGn+kmvNtDJyOFbtapkeyX49DUQjcpn/w4HRdZEhXF8zyRuiqGOdpgY4Ej Q4mqQI/sJyxxNv0UYHSa3PtmewkJFsaVUTGWrxvPmjdTYwv1GAxaM1weWZ1TBcl97hw5 7/9Q== X-Gm-Message-State: AOAM532tk9Ap3JDPs73l/J6Qw8dFMr5vP6VcTFgiY16U2jl/BJCYy7jy p78ZxPXtLPvN6ZXlHqDoNgAyAsd0lPfCJskcqoI= X-Google-Smtp-Source: ABdhPJzGHtrZYTlveNMVoMuPx91TFtQ3u7mjFxQxJ3TsStIaJcAhmSH+9VDAQVRoAoJLXWuNpvbPhA== X-Received: by 2002:a63:da58:: with SMTP id l24mr27965915pgj.178.1608023826274; Tue, 15 Dec 2020 01:17:06 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id e8sm19193256pfn.176.2020.12.15.01.17.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Dec 2020 01:17:05 -0800 (PST) From: Vincent Chen To: libc-alpha@sourceware.org, palmer@dabbelt.com, darius@bluespec.com, dj@redhat.com Subject: [PATCH v2 2/2] riscv: Initialize $gp before resolving the IRELATIVE relocation Date: Tue, 15 Dec 2020 17:16:40 +0800 Message-Id: <1608023800-14560-3-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1608023800-14560-1-git-send-email-vincent.chen@sifive.com> References: <1608023800-14560-1-git-send-email-vincent.chen@sifive.com> X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: nelson.chu@sifive.com, Vincent Chen , paul.walmsley@sifive.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The $gp register may be used to access the global variable in the PDE program, so the $gp register should be initialized before executing the IFUNC resolver of PDE program to avoid unexpected error occurs. --- sysdeps/riscv/dl-machine.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index 0ae45dd0c3..211835f0e6 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -339,8 +339,28 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile) gotplt[0] = (ElfW(Addr)) &_dl_runtime_resolve; gotplt[1] = (ElfW(Addr)) l; } -#endif + if (l->l_type == lt_executable) + { + /* The __global_pointer$ may not be defined by the linker if the + $gp register does not be used to access the global variable + in the executable program. Therefore, the search symbol is + set to a weak symbol to avoid we error out if the + __global_pointer$ is not found. */ + ElfW(Sym) gp_sym = { 0 }; + gp_sym.st_info = (unsigned char) ELFW (ST_INFO (STB_WEAK, STT_NOTYPE)); + + const ElfW(Sym) *ref = &gp_sym; + _dl_lookup_symbol_x ("__global_pointer$", l, &ref, + l->l_scope, NULL, 0, 0, NULL); + if (ref) + asm ( + "mv gp, %0\n" + : + : "r" (ref->st_value) + ); + } +#endif return lazy; }