From patchwork Wed Jun 18 05:54:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 897540 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f54:0:b0:3a4:ee3f:8f15 with SMTP id cm20csp135154wrb; Tue, 17 Jun 2025 22:57:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVS2Gm6PckNk14+B5cA3Tm5qlS7BEWxWFYY7vSQXJArReo7SglebXlwJptiPKS0sI+WkYOEmQ==@linaro.org X-Google-Smtp-Source: AGHT+IHsLrgTCXZqkDvY1mHIXKQhUcCqop8cNTZIPLQ+rFPTQicwQxfh8rS0PDwA/dm7PooXpuph X-Received: by 2002:a05:622a:316:b0:4a6:f587:ade2 with SMTP id d75a77b69052e-4a73c4bece2mr272767801cf.18.1750226232037; Tue, 17 Jun 2025 22:57:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1750226232; cv=pass; d=google.com; s=arc-20240605; b=lsD+/87qMheGM6yQMccgoTUHYku/vOvZyvOkkR4Wpzw5xkeh0BKeWj55chuV+6fxM9 496lNe6rp3aOvKnxO/TVow5jWzi+Zpz9gCTSpTKeORcHphshYaRI/0u0q49k0sIYWeH3 rwyIkpYuQBk5qQq9HBBFMX4IleM9nPm8qTn2lb8MMwvgIpZ75K/isgiVoMEWCsTYyLPG eTZgRJp6imWN3l61B8OTdQVcIr14H+dZk229gAbHOmKBEwXTYmV8WWrolT32+X7fvpF+ BckP8jQHEfueNp8gaBjrOsuZBfS9qqc9ULjNhPXzfIR9qdYtIlUQd7HnXbkcrchaKLTw nYtg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=8y5ObXPHryGjUlxYenCRlE0BCBBW6Wsi8+LlIvBecTQ=; fh=v0Swrqm/HGzmFfs2Lyn4KGEK39u4NVWMm4qGXeDZh/s=; b=EjAV8tkfwqJ5y97nS0PFEcgVN8/JG6BDwLe0zYThSFOEBYap8c3Y8fbPzjje1Plzpz sc8rGXTd1S7xXBrX+jgcTvBXoqp1UoAQot43fJcTWNBaPmqlgUUIto6cqs+KQA8jS51W B4fLX9gnA0HlvcKC1IRoO905QcV8ToTS+LlMdEEv2CzU2nmrUX+4ZDvBeaSTjVYOm8Wj /dm7TtyAhmoqA/owS4ju20CcShT27PtYQLK3SY1xASnAz9U0WRHVc+qGeMBhpAmnDpw9 jQTNuHiFhFNRhOJRcioJXSbU7FuYdeUyZNIo99iA+kxTStZ/bXHXhrOPA6NTCbsZUmjt Iyag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ll07CNox; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gdb-patches-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d75a77b69052e-4a72a4b1e5bsi135275591cf.309.2025.06.17.22.57.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 22:57:12 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ll07CNox; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gdb-patches-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 634673858C24 for ; Wed, 18 Jun 2025 05:57:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 634673858C24 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=ll07CNox X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 5C9F83858C24 for ; Wed, 18 Jun 2025 05:54:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5C9F83858C24 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5C9F83858C24 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::434 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750226098; cv=none; b=N6pEwC85SBSLz+g3MOHBwV0n+tigK70ZW2BLXcyu7AsHqNAFL755/aeMun7v5VSm00UQW5HKQLiZ9QM23PF483XAy7/4XUIPh9Gh8ekqX1/y26NpN1sSZS4196RtJ+x9VybWXGTjMu4/b9CgCC7CPNNefh2B1FMez5onSXqLoWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750226098; c=relaxed/simple; bh=hH5hHTjpH6ueJ4Q0kcRndynfmQlWfho3Sk4Fm1kPcMo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=n8rZTcDSilRhk9nfb64oAGXpXcgVhsPteJUgzEZqlPp2qyBwXJRYZXagjO4tYWwzAM0IwDPVu7PtNvFq6L+FzpglO70smZLiiOS2M3LY8bTFrvYmSPoOsR+zWj14tyqV9cFgvcMAN/m3IQmytPBqE1l76mQp3mRI3c0grTrs3zk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C9F83858C24 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-74264d1832eso7307424b3a.0 for ; Tue, 17 Jun 2025 22:54:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1750226097; x=1750830897; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8y5ObXPHryGjUlxYenCRlE0BCBBW6Wsi8+LlIvBecTQ=; b=ll07CNoxRCqqTQ/DzDNWS6vO3Ok2jzjnXRKsEVdTHPS7V9hf+VEsydRThQz5As/0aO DD248OyPxzFHFE0MwTIdQkIb/1twoySKh8aABdPdXHeJ9mm8apGcYFkmNcM6wTZfe+jI TV9Oirrm8ZkmLlAmWF1dyiYP/9EUGOD4q+/5okARGlup2ElEHHbY6/GDWBBFu4+vSy8J gyaUVsIg+qb31230RlVNbUu3p1CpGap8pfuLL/2Pc3Eoe+T6mQT3Y+8rdhHIy9jTu8Ai kScl1GiS6cn6ghF7FGWPkCSjVtYX+/eOc3mIDFJXeMPiZ2hHfcLJOjKowRY7jApzb+6q iZxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750226097; x=1750830897; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8y5ObXPHryGjUlxYenCRlE0BCBBW6Wsi8+LlIvBecTQ=; b=LtGqV9OCFD6gddYmp4uh3xQ1hZe7xpSM/uPP1lZWmO0Ym/PVwMPeRpnTwJsvLg/Q/o 08d2Pv2d8j5wEpVlHvBuZDl2eWL8t8ryzEOPR/fQv/qMalq+uvB6qlKi0CXZDJ6SGJpf JJhXkCo7HqrjjHgQBugwqiLe9Kb1I3Og+kEq7F70Gr04au54mnnRtD1zFMPUcYxWiYv6 HFbn4MaeFj9UBxr4HsoPBw3OvRWDH/YJi7oJfna5/4YFKfAeSHmGXR9AZHd9Kyymk8e7 RekmMofA2fGQ+PJ44RtI/+ZQWlXT8DawBEpQqsIV4Xf9iir9je/MdOivu0GeZWWCMWxB J6fw== X-Gm-Message-State: AOJu0Yz22hZV4yFicFeM7OkQF4RWZYQkg3qNqwfMu8eBwnUV6m39i62w RfgbZo43QqWAd3ivozJt+IcjnGA14FvEx8239pf6tmWUJVGYAACsgxLsnPQGt9v4Oel1h63VcIR KDWU/ X-Gm-Gg: ASbGnctIUCwVJTCZXDmXV6p/+dwt5cfnZ3V3eKmiZwrw8ZlyNYea0VajYSYIHRP7dmF jdMzmc5tNDaknjxRBFzsaqiPA+o0IbG/L2wvu2BYoHLMnIXhHyWk5/0eWEV8fktiZvCb0dy7C6q LTp978Y/tqAfpJCg2eI7mqEWkpDBFM08567SgkNY9zn85hD8k2s5+aj6C5FLBqiaZJrknYNx5t0 fJYpiiboDysAAqD11Vrmu+KDcSgHwf68eVXx7lDkp07/khMJEY6RrgakPkeMOZRI07N69opFzz6 w8A/Sun3AF2SwsHkwlWI4Rd7XDLHClom6mNpp28w6VUDaBHuvTlph1wUz4PYYUgcokuknWlnSs/ YACg= X-Received: by 2002:a05:6a00:f8b:b0:742:da7c:3f30 with SMTP id d2e1a72fcca58-7489cfde786mr22910468b3a.19.1750226097296; Tue, 17 Jun 2025 22:54:57 -0700 (PDT) Received: from localhost ([2804:14d:7e39:88d6:ae12:97ab:a9:43c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74890006294sm10046351b3a.47.2025.06.17.22.54.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 22:54:56 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Eli Zaretskii , "Schimpe, Christina" , Luis Machado Subject: [PATCH v2 3/9] gdb, gdbarch: Introduce gdbarch method to get the shadow stack pointer. Date: Wed, 18 Jun 2025 02:54:39 -0300 Message-ID: <20250618055445.709416-4-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250618055445.709416-1-thiago.bauermann@linaro.org> References: <20250618055445.709416-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patch=linaro.org@sourceware.org From: Christina Schimpe This patch is required by the following commit. Reviewed-by: Thiago Jung Bauermann --- gdb/arch-utils.c | 10 ++++++++++ gdb/arch-utils.h | 5 +++++ gdb/gdbarch-gen.c | 22 ++++++++++++++++++++++ gdb/gdbarch-gen.h | 10 ++++++++++ gdb/gdbarch_components.py | 15 +++++++++++++++ 5 files changed, 62 insertions(+) This is exactly: [PATCH v4 10/11] gdb, gdbarch: Introduce gdbarch method to get the shadow stack pointer. https://inbox.sourceware.org/gdb-patches/20250617121147.1956686-11-christina.schimpe@intel.com/ diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c index be0494fba1e3..c86df72a6d84 100644 --- a/gdb/arch-utils.c +++ b/gdb/arch-utils.c @@ -1218,6 +1218,16 @@ default_gdbarch_return_value readbuf, writebuf); } +/* See arch-utils.h. */ + +std::optional +default_get_shadow_stack_pointer (gdbarch *gdbarch, regcache *regcache, + bool &shadow_stack_enabled) +{ + shadow_stack_enabled = false; + return {}; +} + obstack *gdbarch_obstack (gdbarch *arch) { return &arch->obstack; diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h index 1509cb7441e6..14a84b747332 100644 --- a/gdb/arch-utils.h +++ b/gdb/arch-utils.h @@ -414,4 +414,9 @@ extern enum return_value_convention default_gdbarch_return_value struct regcache *regcache, struct value **read_value, const gdb_byte *writebuf); +/* Default implementation of gdbarch default_get_shadow_stack_pointer + method. */ +extern std::optional default_get_shadow_stack_pointer + (gdbarch *gdbarch, regcache *regcache, bool &shadow_stack_enabled); + #endif /* GDB_ARCH_UTILS_H */ diff --git a/gdb/gdbarch-gen.c b/gdb/gdbarch-gen.c index 3ca19c427a31..4490d53a940a 100644 --- a/gdb/gdbarch-gen.c +++ b/gdb/gdbarch-gen.c @@ -263,6 +263,7 @@ struct gdbarch gdbarch_use_target_description_from_corefile_notes_ftype *use_target_description_from_corefile_notes = default_use_target_description_from_corefile_notes; gdbarch_core_parse_exec_context_ftype *core_parse_exec_context = default_core_parse_exec_context; gdbarch_shadow_stack_push_ftype *shadow_stack_push = nullptr; + gdbarch_get_shadow_stack_pointer_ftype *get_shadow_stack_pointer = default_get_shadow_stack_pointer; }; /* Create a new ``struct gdbarch'' based on information provided by @@ -537,6 +538,7 @@ verify_gdbarch (struct gdbarch *gdbarch) /* Skip verify of use_target_description_from_corefile_notes, invalid_p == 0. */ /* Skip verify of core_parse_exec_context, invalid_p == 0. */ /* Skip verify of shadow_stack_push, has predicate. */ + /* Skip verify of get_shadow_stack_pointer, invalid_p == 0. */ if (!log.empty ()) internal_error (_("verify_gdbarch: the following are invalid ...%s"), log.c_str ()); @@ -1414,6 +1416,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file) gdb_printf (file, "gdbarch_dump: shadow_stack_push = <%s>\n", host_address_to_string (gdbarch->shadow_stack_push)); + gdb_printf (file, + "gdbarch_dump: get_shadow_stack_pointer = <%s>\n", + host_address_to_string (gdbarch->get_shadow_stack_pointer)); if (gdbarch->dump_tdep != NULL) gdbarch->dump_tdep (gdbarch, file); } @@ -5583,3 +5588,20 @@ set_gdbarch_shadow_stack_push (struct gdbarch *gdbarch, { gdbarch->shadow_stack_push = shadow_stack_push; } + +std::optional +gdbarch_get_shadow_stack_pointer (struct gdbarch *gdbarch, regcache *regcache, bool &shadow_stack_enabled) +{ + gdb_assert (gdbarch != NULL); + gdb_assert (gdbarch->get_shadow_stack_pointer != NULL); + if (gdbarch_debug >= 2) + gdb_printf (gdb_stdlog, "gdbarch_get_shadow_stack_pointer called\n"); + return gdbarch->get_shadow_stack_pointer (gdbarch, regcache, shadow_stack_enabled); +} + +void +set_gdbarch_get_shadow_stack_pointer (struct gdbarch *gdbarch, + gdbarch_get_shadow_stack_pointer_ftype get_shadow_stack_pointer) +{ + gdbarch->get_shadow_stack_pointer = get_shadow_stack_pointer; +} diff --git a/gdb/gdbarch-gen.h b/gdb/gdbarch-gen.h index c276cd66d716..b8d1df94f598 100644 --- a/gdb/gdbarch-gen.h +++ b/gdb/gdbarch-gen.h @@ -1806,6 +1806,8 @@ extern void set_gdbarch_core_parse_exec_context (struct gdbarch *gdbarch, gdbarc technologies. For example, the Intel Control-Flow Enforcement Technology (Intel CET) on x86 provides a shadow stack and indirect branch tracking. To enable inferior calls the function shadow_stack_push has to be provided. + The method get_shadow_stack_pointer has to be provided to enable displaced + stepping. Push the address NEW_ADDR on the shadow stack and update the shadow stack pointer. */ @@ -1815,3 +1817,11 @@ extern bool gdbarch_shadow_stack_push_p (struct gdbarch *gdbarch); typedef void (gdbarch_shadow_stack_push_ftype) (struct gdbarch *gdbarch, CORE_ADDR new_addr, regcache *regcache); extern void gdbarch_shadow_stack_push (struct gdbarch *gdbarch, CORE_ADDR new_addr, regcache *regcache); extern void set_gdbarch_shadow_stack_push (struct gdbarch *gdbarch, gdbarch_shadow_stack_push_ftype *shadow_stack_push); + +/* If possible, return the shadow stack pointer. On some architectures, the + shadow stack pointer is available even if the feature is disabled. To + return the shadow stack enablement state configure SHADOW_STACK_ENABLED. */ + +typedef std::optional (gdbarch_get_shadow_stack_pointer_ftype) (struct gdbarch *gdbarch, regcache *regcache, bool &shadow_stack_enabled); +extern std::optional gdbarch_get_shadow_stack_pointer (struct gdbarch *gdbarch, regcache *regcache, bool &shadow_stack_enabled); +extern void set_gdbarch_get_shadow_stack_pointer (struct gdbarch *gdbarch, gdbarch_get_shadow_stack_pointer_ftype *get_shadow_stack_pointer); diff --git a/gdb/gdbarch_components.py b/gdb/gdbarch_components.py index ab685b14ec7d..ccbdcaf07c97 100644 --- a/gdb/gdbarch_components.py +++ b/gdb/gdbarch_components.py @@ -2855,6 +2855,8 @@ Some targets support special hardware-assisted control-flow protection technologies. For example, the Intel Control-Flow Enforcement Technology (Intel CET) on x86 provides a shadow stack and indirect branch tracking. To enable inferior calls the function shadow_stack_push has to be provided. +The method get_shadow_stack_pointer has to be provided to enable displaced +stepping. Push the address NEW_ADDR on the shadow stack and update the shadow stack pointer. @@ -2864,3 +2866,16 @@ pointer. params=[("CORE_ADDR", "new_addr"), ("regcache *", "regcache")], predicate=True, ) + +Method( + comment=""" +If possible, return the shadow stack pointer. On some architectures, the +shadow stack pointer is available even if the feature is disabled. To +return the shadow stack enablement state configure SHADOW_STACK_ENABLED. +""", + type="std::optional", + name="get_shadow_stack_pointer", + params=[("regcache *", "regcache"), ("bool &", "shadow_stack_enabled")], + predefault="default_get_shadow_stack_pointer", + invalid=False, +)