From patchwork Mon Sep 18 21:26:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Machado X-Patchwork-Id: 76324 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 928E23853D15 for ; Mon, 18 Sep 2023 21:30:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 928E23853D15 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695072607; bh=mnYzuh2bHbaa1WJoZGINrxYKB0wUeKyN7riCChuD9BY=; 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=yJ4Dq20Mb/sObpU5sy3vdogj7qQaYLQZswHZkVxfUwfQzvAkcdmxzBmlltEGFAw22 EGTMn2NvMpUMFN2YMimqWzItatkXjYMTuUq+vkbF00qNRC4GkSnY6AT9zKxlA/EiF3 Vwtt66Ewx6Qh+i9MGPHzhdQTReDHZ+CUHFSUxe+4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on2058.outbound.protection.outlook.com [40.107.7.58]) by sourceware.org (Postfix) with ESMTPS id 4C8DD3857004 for ; Mon, 18 Sep 2023 21:27:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C8DD3857004 Received: from DUZPR01CA0178.eurprd01.prod.exchangelabs.com (2603:10a6:10:4b3::20) by DU2PR08MB10263.eurprd08.prod.outlook.com (2603:10a6:10:491::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.21; Mon, 18 Sep 2023 21:27:19 +0000 Received: from DBAEUR03FT025.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:4b3:cafe::3a) by DUZPR01CA0178.outlook.office365.com (2603:10a6:10:4b3::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.26 via Frontend Transport; Mon, 18 Sep 2023 21:27:19 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DBAEUR03FT025.mail.protection.outlook.com (100.127.142.226) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.16 via Frontend Transport; Mon, 18 Sep 2023 21:27:19 +0000 Received: ("Tessian outbound 169aaa6bf2b7:v175"); Mon, 18 Sep 2023 21:27:19 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 9df5e70ec9a5978e X-CR-MTA-TID: 64aa7808 Received: from 67ddc84cc824.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 952A1911-448D-4781-AAF8-BABD1BBFB30E.1; Mon, 18 Sep 2023 21:27:13 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 67ddc84cc824.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Mon, 18 Sep 2023 21:27:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MxtEBwlAuvWpRmGVI2V137IrDs75CN7qM7RZQkZzG64TH7/RQxopmGe1nm9LC+E/TsAcWKBeVfgr+EpbdwM0HNrInEBrV6QOlYI7JXJC8YrcvAK6eX4pjiD9zCYl15H8jyGP6yDTtZ10h7M7OxtX965mw3pQ1YP927yazmJOri0fdxU6RN3D7tKO5tcUc+bz7KWvcr9yZABLu7/zVyMR/M0nudUhu+6WXqF6LSyEHr1I73lqtA5eYv9NuYYPEillhWfFI5TYI4uFyQoxr8DQo5LE1ko/YyHAhYGJvTuC14/FF992MACFkOG51ibKDVDfCFpXlE3n01mXM3JzefkYVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=mnYzuh2bHbaa1WJoZGINrxYKB0wUeKyN7riCChuD9BY=; b=lXqGTLSv5s7KGBvuz6lg9PgNPVDEOGRjid+twvSs9rsg63In9mgXeOlQMLhWOGMfeGSAiWXLATi35lW960hIv57DYJgHdjigrf842qjVN2vTLtMNp8pDAHZZByGx5IpxrCFTVRp5AX7nr+UQ/N/tHy2GN301Lr5eLxrtZ1zSEq8cUkH5gjoY4qo1ctmsCZDFOecIiwjCdyMwekhn4VicIg457Klp7XnO2vOZVFMDP9Ed26AXzi76svpvwgClCgIk6bmQtaOohtyrhyMYIk1NM9B8UWY0GKW2UBpO5f2/7sS9gsOAHg9E4rFo9IIkTdAQleOn+qTY2dUWt0p71MKiIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none Received: from AM6P193CA0128.EURP193.PROD.OUTLOOK.COM (2603:10a6:209:85::33) by GV2PR08MB8512.eurprd08.prod.outlook.com (2603:10a6:150:c2::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.23; Mon, 18 Sep 2023 21:27:08 +0000 Received: from AM7EUR03FT009.eop-EUR03.prod.protection.outlook.com (2603:10a6:209:85:cafe::6a) by AM6P193CA0128.outlook.office365.com (2603:10a6:209:85::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.27 via Frontend Transport; Mon, 18 Sep 2023 21:27:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by AM7EUR03FT009.mail.protection.outlook.com (100.127.140.130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6813.16 via Frontend Transport; Mon, 18 Sep 2023 21:27:08 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Mon, 18 Sep 2023 21:27:07 +0000 Received: from e129171.arm.com (10.57.64.238) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server id 15.1.2507.27 via Frontend Transport; Mon, 18 Sep 2023 21:27:07 +0000 To: Subject: [PATCH v7 15/18] [gdb/generic] corefile/bug: Add hook to control the use of target description notes from corefiles Date: Mon, 18 Sep 2023 22:26:48 +0100 Message-ID: <20230918212651.660141-16-luis.machado@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230918212651.660141-1-luis.machado@arm.com> References: <20230918212651.660141-1-luis.machado@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: AM7EUR03FT009:EE_|GV2PR08MB8512:EE_|DBAEUR03FT025:EE_|DU2PR08MB10263:EE_ X-MS-Office365-Filtering-Correlation-Id: b9208501-4dd6-48fd-24c2-08dbb88e0a0b x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 9AHZLDbLklI6VeM1In+ZM92+7q8sdVFvW/9hObBITkegflF7y0LZewZXyA9gNSGYlyAEoViArdExUBF4gwLfswNME5hwxF9xJKYnJLzUv6Xcq9AIg9bhbv9CbbxEoecP2SnK0sowPjIJpg9K6dYyb41pkYXOsF0D8V3ZiCcVsxgHDEytFNhSSMZA9nJwxjRrFcqFLwPQLuZt1XkKQPA2OFYF4Vdzuf70kTrpHbgVKQNGPi2VLEckZmpBp+PhW4BQT0BdWdRy7kBMvYobTpEZvbGOLCFKHFICMuYN6n8Z49fJl7LZYHH1CbbJ8hySCm/Yep/9vRDOauxxZgiOmKiwkgnFuftj7gMF7dem/CM4vhJe1Y4G0TKunCS83JCsYdrJtjCM4z00QnoaQRxPLBO8YDMD0Wuc4R7Y3yIFcpCgw3hfJdXheqixF5F7vlGWiiZhNRoCa+S7soyVbE6t8vpdq3HEMkFv7j5eapqGaTIX154pddVeQJOBsKazQNaCgssRVTJfbHVygNn4PqGRZdeAJSkBwC3AGMK5/Ye+ABQIyFDGe991yq/YWRsj/RiYjFFQK8g8etgOKvjHX7sXZmncsFMrGMgCxw7qWQ2rdAwSzZQDi+rllL+LgHpS3Xlke+xebwGgmCG+hehtfdSr/Zcojx+hOxGz7H1U/yQ04PTEixqI7pUy36aDjO1V+TdMfQ2830Xv7u6G7ew/RKPWuRH0CixFP8B5i/slcB+xjjgIYC3ajV7ZE1ChvjxMbkhxH+Gd5fmXcmdlY6fIjRtHowodRsCAcPY811zBwZhdbLw8PAQ= X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230031)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199024)(1800799009)(186009)(82310400011)(40470700004)(36840700001)(46966006)(40480700001)(44832011)(7696005)(86362001)(54906003)(316002)(41300700001)(70206006)(70586007)(5660300002)(6666004)(478600001)(6916009)(8936002)(2616005)(8676002)(356005)(81166007)(26005)(82740400003)(2906002)(36860700001)(36756003)(30864003)(336012)(426003)(47076005)(40460700003)(1076003)(4326008)(83380400001)(41533002)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR08MB8512 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DBAEUR03FT025.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: fd0d1775-bc9a-4321-68ef-08dbb88e0343 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xPpM3U5qEb2lkK5XH4xonoZkUv6aeD7tEyS1vhA9CpAkiIvTbmbBolIJBTh0/JuXOFiqsRASKa8GNRmYM038Y0esH2jfBedvTM9J+u50fQdotezstqyZBGdEDsJ9ZRTKiyqevYso0TDc0KB/4vWpJo/4Q12WTSvWS+JfCExtbwDHkD7381n+nTJ9Ji6EPMfMQ+gswj8jtVNLJgaLg7LACeHR7/RHbIqUZlh3ylIz3uEfXIq9fGOc6y2INrsknF3FBnKyg7AsBRilf/Y45aBFjI2FDZuXhm5ziwYKN7/Z3FdguNTvhv/rND/5MFFNdBcXp0iSIE6/ltCz4ykh+1z6J9iyTs/Q9sl+N++pAbNr27YBv9c1JDyKGfDm9KtEnGxAg77darD7eOEAd01CfiNHkgF2wiHiCvWfxbjTMa8iV4uAf+vLk1VESQcT7pmem008l9YDRV5GLkJEeCkBmlt4WW/TrfGOsyGmyn4Cz7b/Ex5O2bep6Py6v+CZuKQQ4CFi5cg2RqPke6/gjsOGw9Nw2f+XiSwmwmnIrKlKV/43KTT61ksLd2l+PkUGxTBVSJ2wFqwIkFY1WtPEv4g5bqmEBjxKOl046ugd4ge2hl+fjrFW9m1lqjTgC+srKoyGCjdcno6xiMjKozK3ago5EltkzbNy24UamFHHM4Iv3udwupkoSnY8xYBPOomvl4wTV++9BwxboWQJuZwxlqiI0ykIJXgqht3XLCTQof09nUuDGV0VmLXfCtX1qXsk36m7p8FU9Qil51to+fCRDjJhpOJSAw== X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230031)(4636009)(39850400004)(346002)(136003)(376002)(396003)(451199024)(186009)(1800799009)(82310400011)(36840700001)(40470700004)(46966006)(83380400001)(5660300002)(4326008)(6666004)(7696005)(82740400003)(40480700001)(36756003)(81166007)(86362001)(107886003)(36860700001)(1076003)(2616005)(26005)(2906002)(336012)(426003)(30864003)(478600001)(6916009)(41300700001)(47076005)(40460700003)(8676002)(8936002)(70586007)(70206006)(316002)(44832011)(54906003)(41533002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2023 21:27:19.6270 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b9208501-4dd6-48fd-24c2-08dbb88e0a0b X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DBAEUR03FT025.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR08MB10263 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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: , X-Patchwork-Original-From: Luis Machado via Gdb-patches From: Luis Machado Reply-To: Luis Machado Cc: thiago.bauermann@linaro.org Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" New entry in v7. --- Due to the nature of the AArch64 SVE/SME extensions in GDB, each thread can potentially have distinct target descriptions/gdbarches. When loading a gcore-generated core file, at the moment GDB gives priority to the target description dumped to NT_GDB_TDESC. Though technically correct for most targets, it doesn't work correctly for AArch64 with SVE or SME support. The correct approach for AArch64/Linux is to either have per-thread target description notes in the corefiles or to rely on the gdbarch_core_read_description hook, so it can figure out the proper target description for a given thread based on the various available register notes. The former, although more correct, doesn't address the case of existing gdb's that only output a single target description note. This patch goes for the latter, and adds a new gdbarch hook to conditionalize the use of the corefile target description note. The hook is called use_target_description_from_corefile_notes. The hook defaults to returning true, meaning targets will use the corefile target description note. AArch64 Linux overrides the hook to return false when it detects any of the SVE or SME register notes in the corefile. Otherwise it should be fine for AArch64 Linux to use the corefile target description note. When we support per-thread target description notes, then we can augment the AArch64 Linux hook to rely on those notes. Regression-tested on aarch64-linux Ubuntu 22.04/20.04. --- gdb/aarch64-linux-tdep.c | 33 ++++++++++++++++++++++++++ gdb/arch-utils.c | 10 ++++++++ gdb/arch-utils.h | 6 +++++ gdb/corelow.c | 50 ++++++++++++++++++++++++--------------- gdb/gdbarch-gen.h | 14 +++++++++++ gdb/gdbarch.c | 22 +++++++++++++++++ gdb/gdbarch_components.py | 19 +++++++++++++++ 7 files changed, 135 insertions(+), 19 deletions(-) diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c index 47e5e1db641..21ac7ebdc56 100644 --- a/gdb/aarch64-linux-tdep.c +++ b/gdb/aarch64-linux-tdep.c @@ -2199,6 +2199,33 @@ aarch64_linux_decode_memtag_section (struct gdbarch *gdbarch, return tags; } +/* AArch64 Linux implementation of the + gdbarch_use_target_description_from_corefile_notes hook. */ + +static bool +aarch64_use_target_description_from_corefile_notes (gdbarch *gdbarch, + bfd *obfd) +{ + /* Sanity check. */ + gdb_assert (obfd != nullptr); + + /* If the corefile contains any SVE or SME register data, we don't want to + use the target description note, as it may be incorrect. + + Currently the target description note contains a potentially incorrect + target description if the originating program changed the SVE or SME + vector lengths mid-execution. + + Once we support per-thread target description notes in the corefiles, we + can always trust those notes whenever they are available. */ + if (bfd_get_section_by_name (obfd, ".reg-aarch-sve") != nullptr + || bfd_get_section_by_name (obfd, ".reg-aarch-za") != nullptr + || bfd_get_section_by_name (obfd, ".reg-aarch-zt") != nullptr) + return false; + + return true; +} + static void aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) { @@ -2469,6 +2496,12 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) aarch64_displaced_step_hw_singlestep); set_gdbarch_gcc_target_options (gdbarch, aarch64_linux_gcc_target_options); + + /* Hook to decide if the target description should be obtained from + corefile target description note(s) or inferred from the corefile + sections. */ + set_gdbarch_use_target_description_from_corefile_notes (gdbarch, + aarch64_use_target_description_from_corefile_notes); } #if GDB_SELF_TEST diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c index ee34fc07d33..c1d2c939eb9 100644 --- a/gdb/arch-utils.c +++ b/gdb/arch-utils.c @@ -1092,6 +1092,16 @@ default_read_core_file_mappings { } +/* See arch-utils.h. */ +bool +default_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, + struct bfd *obfd) +{ + /* Always trust the corefile target description contained in the target + description note. */ + return true; +} + CORE_ADDR default_get_return_buf_addr (struct type *val_type, frame_info_ptr cur_frame) { diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h index 2bdc3251c9c..5df3de7b5d9 100644 --- a/gdb/arch-utils.h +++ b/gdb/arch-utils.h @@ -305,6 +305,12 @@ extern void default_read_core_file_mappings read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb); +/* Default implementation of gdbarch + use_target_description_from_corefile_notes. */ +extern bool default_use_target_description_from_corefile_notes + (struct gdbarch *gdbarch, + struct bfd *obfd); + /* Default implementation of gdbarch default_get_return_buf_addr method. */ extern CORE_ADDR default_get_return_buf_addr (struct type *val_typegdbarch, frame_info_ptr cur_frame); diff --git a/gdb/corelow.c b/gdb/corelow.c index 439270f5559..114ce3054d5 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -1229,35 +1229,47 @@ core_target::thread_alive (ptid_t ptid) const struct target_desc * core_target::read_description () { - /* If the core file contains a target description note then we will use - that in preference to anything else. */ - bfd_size_type tdesc_note_size = 0; - struct bfd_section *tdesc_note_section - = bfd_get_section_by_name (core_bfd, ".gdb-tdesc"); - if (tdesc_note_section != nullptr) - tdesc_note_size = bfd_section_size (tdesc_note_section); - if (tdesc_note_size > 0) + /* First check whether the target wants us to use the corefile target + description notes. */ + if (gdbarch_use_target_description_from_corefile_notes (m_core_gdbarch, + core_bfd)) { - gdb::char_vector contents (tdesc_note_size + 1); - if (bfd_get_section_contents (core_bfd, tdesc_note_section, - contents.data (), (file_ptr) 0, - tdesc_note_size)) + /* If the core file contains a target description note then go ahead and + use that. */ + bfd_size_type tdesc_note_size = 0; + struct bfd_section *tdesc_note_section + = bfd_get_section_by_name (core_bfd, ".gdb-tdesc"); + if (tdesc_note_section != nullptr) + tdesc_note_size = bfd_section_size (tdesc_note_section); + if (tdesc_note_size > 0) { - /* Ensure we have a null terminator. */ - contents[tdesc_note_size] = '\0'; - const struct target_desc *result - = string_read_description_xml (contents.data ()); - if (result != nullptr) - return result; + gdb::char_vector contents (tdesc_note_size + 1); + if (bfd_get_section_contents (core_bfd, tdesc_note_section, + contents.data (), (file_ptr) 0, + tdesc_note_size)) + { + /* Ensure we have a null terminator. */ + contents[tdesc_note_size] = '\0'; + const struct target_desc *result + = string_read_description_xml (contents.data ()); + if (result != nullptr) + return result; + } } } + /* If the architecture provides a corefile target description hook, use + it now. Even if the core file contains a target description in a note + section, it is not useful for targets that can potentially have distinct + descriptions for each thread. One example is AArch64's SVE/SME + extensions that allow per-thread vector length changes, resulting in + registers with different sizes. */ if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch)) { const struct target_desc *result; result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd); - if (result != NULL) + if (result != nullptr) return result; } diff --git a/gdb/gdbarch-gen.h b/gdb/gdbarch-gen.h index d62eefa1c5b..33276aa1c43 100644 --- a/gdb/gdbarch-gen.h +++ b/gdb/gdbarch-gen.h @@ -1717,3 +1717,17 @@ extern void set_gdbarch_get_pc_address_flags (struct gdbarch *gdbarch, gdbarch_g typedef void (gdbarch_read_core_file_mappings_ftype) (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb); extern void gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb); extern void set_gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, gdbarch_read_core_file_mappings_ftype *read_core_file_mappings); + +/* Return true if the target description for all threads should be read from the + target description core file note(s). Return false if the target description + for all threads should be inferred from the core file contents/sections. + + The corefile's bfd is passed through OBFD. + + This hook should be used by targets that can have distinct target descriptions + for each thread when the core file only holds a single target description + note. */ + +typedef bool (gdbarch_use_target_description_from_corefile_notes_ftype) (struct gdbarch *gdbarch, struct bfd *obfd); +extern bool gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, struct bfd *obfd); +extern void set_gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, gdbarch_use_target_description_from_corefile_notes_ftype *use_target_description_from_corefile_notes); diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c index 1fc254d3d6e..ee868908598 100644 --- a/gdb/gdbarch.c +++ b/gdb/gdbarch.c @@ -256,6 +256,7 @@ struct gdbarch gdbarch_type_align_ftype *type_align = default_type_align; gdbarch_get_pc_address_flags_ftype *get_pc_address_flags = default_get_pc_address_flags; gdbarch_read_core_file_mappings_ftype *read_core_file_mappings = default_read_core_file_mappings; + gdbarch_use_target_description_from_corefile_notes_ftype *use_target_description_from_corefile_notes = default_use_target_description_from_corefile_notes; }; /* Create a new ``struct gdbarch'' based on information provided by @@ -523,6 +524,7 @@ verify_gdbarch (struct gdbarch *gdbarch) /* Skip verify of type_align, invalid_p == 0 */ /* Skip verify of get_pc_address_flags, invalid_p == 0 */ /* Skip verify of read_core_file_mappings, invalid_p == 0 */ + /* Skip verify of use_target_description_from_corefile_notes, invalid_p == 0 */ if (!log.empty ()) internal_error (_("verify_gdbarch: the following are invalid ...%s"), log.c_str ()); @@ -1373,6 +1375,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file) gdb_printf (file, "gdbarch_dump: read_core_file_mappings = <%s>\n", host_address_to_string (gdbarch->read_core_file_mappings)); + gdb_printf (file, + "gdbarch_dump: use_target_description_from_corefile_notes = <%s>\n", + host_address_to_string (gdbarch->use_target_description_from_corefile_notes)); if (gdbarch->dump_tdep != NULL) gdbarch->dump_tdep (gdbarch, file); } @@ -5409,3 +5414,20 @@ set_gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, { gdbarch->read_core_file_mappings = read_core_file_mappings; } + +bool +gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, struct bfd *obfd) +{ + gdb_assert (gdbarch != NULL); + gdb_assert (gdbarch->use_target_description_from_corefile_notes != NULL); + if (gdbarch_debug >= 2) + gdb_printf (gdb_stdlog, "gdbarch_use_target_description_from_corefile_notes called\n"); + return gdbarch->use_target_description_from_corefile_notes (gdbarch, obfd); +} + +void +set_gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, + gdbarch_use_target_description_from_corefile_notes_ftype use_target_description_from_corefile_notes) +{ + gdbarch->use_target_description_from_corefile_notes = use_target_description_from_corefile_notes; +} diff --git a/gdb/gdbarch_components.py b/gdb/gdbarch_components.py index 846467b8d83..bbb9b188286 100644 --- a/gdb/gdbarch_components.py +++ b/gdb/gdbarch_components.py @@ -2732,3 +2732,22 @@ Read core file mappings predefault="default_read_core_file_mappings", invalid=False, ) + +Method( + comment=""" +Return true if the target description for all threads should be read from the +target description core file note(s). Return false if the target description +for all threads should be inferred from the core file contents/sections. + +The corefile's bfd is passed through OBFD. + +This hook should be used by targets that can have distinct target descriptions +for each thread when the core file only holds a single target description +note. +""", + type="bool", + name="use_target_description_from_corefile_notes", + params=[("struct bfd *", "obfd")], + predefault="default_use_target_description_from_corefile_notes", + invalid=False, +)