From patchwork Sun Jun 22 01:18:05 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: 899061 Delivered-To: patch@linaro.org Received: by 2002:adf:e506:0:b0:3a6:d909:26ce with SMTP id j6csp367710wrm; Sat, 21 Jun 2025 20:54:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWpofuJy5+Y7E3k8hufNShoI32v0YOcXcpZ/lIuA0uhc334HbV7I52hFGaFlBiWcb459Fa/Hw==@linaro.org X-Google-Smtp-Source: AGHT+IGVFkkcunWwzBSyApMcv7yRyBdjlFbBPed/eEn4NtlKPe88664UtL7InDbNbr/L6LSax9sb X-Received: by 2002:a05:620a:1994:b0:7d2:2a6:2dec with SMTP id af79cd13be357-7d3f9932beemr1377054685a.30.1750564445322; Sat, 21 Jun 2025 20:54:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1750564445; cv=pass; d=google.com; s=arc-20240605; b=ENe534LQkkROMyreKs359bdxrCyDwYIvcqal82A6lchNc+INHvX2rBv+91mxkFjg+a pS/FXLgfob7PJjay6ACPGmxw6y6uGTBFw9iRveousKntYdOZj7qs9TZQ00urBDSuQcXK bJ23FmpH+AMrIoSs1hjbJ+o1xEhcSFJJCegFSSdaXoKXTEiwqrEysgN+flWrivtNlIRK EHTbgLOsk4HsV26FnH1pr6u+JwgOpFdT11Agd48f6byn15XUNr+GTxTKzjW1u4cRXQA8 PfjHH5EbjAGA7Y5AOJJRpNrUBvn99FnhyHPcbXwj/+mTKQVOW8KPEvXaCqkJj0DmwbCO DYfw== 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=KBjxxgkElutIc0XXJ4sdD+BBvNnjRy8jf/25rIPZMyk=; fh=v0Swrqm/HGzmFfs2Lyn4KGEK39u4NVWMm4qGXeDZh/s=; b=JPQt+bLitLOztQ5hokzZQJmfafzJzRT/Q10raWwZtT7pAoKPk4IV3ixSHCnaGS7UgR 4CQe1DCr0krOO8xdVGsSvl2ZXS0p77gjNAI3VQ7XDCdiGgzAFgsEI37LcR+k2eheYcJn pAP3VpBl7O92GfRaJwa5I9DPBiBLjCInYxJmh1/At3qUFoHSVEe7GIb4cunZnkLQdi3+ oworgJd4FnpJno01YFmcVHggaTaO8KEh4TNGlpdcrVRxLR/QGbJXyeAk0VybNw7PzP2A t4i2Vsgoby5BevGf8DjRR24KnOKP+50qWI9FNJd8+C4H1UCkD4AucTCGWEsIeYcwAwlK CeqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="fwHGX/Dd"; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id af79cd13be357-7d3f99aa939si488880585a.155.2025.06.21.20.54.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Jun 2025 20:54:05 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="fwHGX/Dd"; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 AA91639AB4D1 for ; Sun, 22 Jun 2025 02:34:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA91639AB4D1 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=fwHGX/Dd X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id 2875F389377B for ; Sun, 22 Jun 2025 01:18:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2875F389377B 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 2875F389377B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1034 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750555104; cv=none; b=LRYZOgDhAgypetJtjJ3rKMwQKEChhjYBSfLkVIwZyDYDU1TPdLFMvocleQIP3WochUjjoULKjMxtKJh9qUi88vIg8J4tsWZwEE3TgKTMd9uuaKe6+8bTzQcuL2AP1pfnvtK/CfCaZZx9cJKFLDVxf+XXt0Ekc1PJ4tTewCiWxI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750555104; c=relaxed/simple; bh=K7noaGLbw7u34zjpiBvUFPTPbrQHhrJb6WTEAAvYL1k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=paq1ItN2/JssaUBYdvDS0Zvrk4FqoVP7mWMnoXPr/3gnOew5agatJyqlkrBFMH5kNbRua0B0mSsqwATTKnE0sphmyQr2PUvPqwgDkbuAaNW+i+0I+X/jxGrCF782rJ78+ZNOLybssqGJ+VqUclMbe+QzGJNQrTx/099zVDp6d18= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2875F389377B Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-31223a4cddeso2199526a91.1 for ; Sat, 21 Jun 2025 18:18:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1750555103; x=1751159903; 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=KBjxxgkElutIc0XXJ4sdD+BBvNnjRy8jf/25rIPZMyk=; b=fwHGX/DdDjJKN4JATqDlNpECoV5S0NqgOhSjGS8ez9pnqdgaSS6ZO8K9Bbs5VHRwiX sM7TwqYCSPgWcZ6M/iIXEkxL0aurpjAD0jQRivVTtdG2cEPVK7gOcJThoLJA3G+elmHS I0SK/HOI4A8vf7UsiKpXYvfi8eWU/7pl1KRZTRrmxIl2oL6xZQA1WwnvDoWtijk3CUoE G/w8li6TpTs+aWRvrIAFH/kYrb6UqWe62iCf5BmxqkoyMEssW06GNIIs5Tc6YhHqz/7p LdRZnjJTaSNkSXMx/9AvNoK21/BVuVOb2xgGTPnzM7EHmlVoD7JtnH+NHYizPPFe7dSE fSPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750555103; x=1751159903; 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=KBjxxgkElutIc0XXJ4sdD+BBvNnjRy8jf/25rIPZMyk=; b=N+Oiibvkwz9siVefcMJaxMM/ozNAVtbc8bqL87aApZw92UbxukrGoZB8KjzNZpk4av 9wZVd4AXJZXQxK1ABhzG5cYsRfX/sJGYTYLf5JT7FXVu6ObEDU5hnxfK+cm0V0c3lqgW Naiod/4UN1N2jt3kgBs617SoRViEqZmC22LSHAa1oY9NdT7X067q4KFf1CKDdov5UrLk EMpPFRdc1cFafA9KGRNC8qZMEsykNFqJkuyKU00eNfA2oFkrNNbLyVqaCyXrw1psaji2 Pe1nR8zV/fZ2KxJt1MVmOjgAEPtxBGCEfEH01rG+xph0WtH50/mUZgbTk72HmNDKCYMo oDyA== X-Gm-Message-State: AOJu0Ywx3RZuM2QHHG4R+pXS4BwuCHBuJXz2kJc6LknxuJ3q8o80cWbF bbcNB2P285At5jkE4PR+yTKD9VWUGi4SpTT5Re3eEFjNMogUVGjqerqYdJ+D8HPaZoQLqNkRPl9 h5s89 X-Gm-Gg: ASbGncvUAHcQrYEQ07KTB+GXCAYlVDF8ZGuPs6V+Pq0NRKCyyYAb/iBhGcKylKGVQ7s 4Ek6+UhrD6iY2qvPPUTaI+byoH83KhoRkyX8a2BaAcgc8L21772zcsPjFlth6NH7B5SDusG62nK h10ZW0TmT4zbaI0j2euSf5NVskcvWskH3L31z71mTSAQF7CaKM2nUVGuGxCiRMbY0mcIU6rzqm0 J0IpXZV5tjxmMXJlXLqmTywANlF/oCAfM05jusYNzxyfhDE3WMumiWNqWNznVvhcSIF4etRU1DI RArZbWQfOMJZuGyqfnOHnCWg+DCOz7eC8iuAKUu52zOXnGpdDuEGbJ9yy11jp5LgrAeo4wOE X-Received: by 2002:a17:90b:3cc5:b0:312:e9d:3ff2 with SMTP id 98e67ed59e1d1-3159d626574mr13455271a91.7.1750555103063; Sat, 21 Jun 2025 18:18:23 -0700 (PDT) Received: from localhost ([2804:14d:7e39:88d6:79ae:7a30:10f3:dfed]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3159df812bdsm5301832a91.12.2025.06.21.18.18.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Jun 2025 18:18:22 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Eli Zaretskii , "Schimpe, Christina" , Luis Machado Subject: [PATCH v3 3/9] gdb, gdbarch: Introduce gdbarch method to get the shadow stack pointer. Date: Sat, 21 Jun 2025 22:18:05 -0300 Message-ID: <20250622011811.371949-4-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250622011811.371949-1-thiago.bauermann@linaro.org> References: <20250622011811.371949-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 --- 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/ 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(+) 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, +)