From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 5D37D384D3DB for ; Tue, 13 Dec 2022 18:45:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5D37D384D3DB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2BDIho7g019013 for ; Tue, 13 Dec 2022 18:45:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=ys+CVKcfPX0WFeALBHE2JIx/MrGyjSkzcxtujfnpZq0=; b=0E7ZyXultBcBMAA6jtnPbuP11fUTgsyvRSlJ+uyjjJpVpeK2KuUtrTcnLFpqzSBLqufB hQMKFyd4T7N3mvpzTFjdDwUYCdcjIbbLF9FaM+KA2Ai6UxVqls4T8+rbT6i10xpDtiOL g/sp5JXWDHAglUIrBbVmyJX82YvZHPn8ffJpneYSnLMxJqMlNJZmrSEIOubALxVGkEvA uzdXs4g2GCkgJSWfaFoAnaYo5kfldJgnE4V2BG5n9A1UdL17RiFrAu7XIltffPWVquT0 thsrkkW/Nkjw6tspvYNGc2BSiw5qz35JnGMOujkiC3VQ2OwxzWNeEPM8YwqTD33+r+E8 Iw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3mcgw2e66n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 13 Dec 2022 18:45:00 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2BDI6Nk2039967 for ; Tue, 13 Dec 2022 18:45:00 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2102.outbound.protection.outlook.com [104.47.55.102]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3mcgjctsmu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 13 Dec 2022 18:44:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HX8CXBW2QUJWIzjd5beLk+edG1FaEAGTtfKCm0HnSn81OFxgQO63ff7FZhwlirvCwWUM39BzlNTFciGTRApL1uugFHNFKp+ZS4+fv1H7wt1wX87l6yLlB/hXWGO+MuM6SC9K3SMSgdDQUrN1LeG3Zowpmn1DtxSF3plqPCz5So7NZWKAvylCmu/y2P4913UZ4uqIrsN76WutFRbq/8qC3JUP2bS4gEUOgTgpxQ25Fm5mYxiQTARBty11f3cdXEqlrhRlU53ZK6HuiC7C6lXdxF9lg7pwAHelKNG37dc66fPWlmEP8QboPE4xUZl9oLSd0zhsgWL6E27dXAMu+MUCEw== 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=ys+CVKcfPX0WFeALBHE2JIx/MrGyjSkzcxtujfnpZq0=; b=HZn47Uzw+cNR64yebfR1AHB8tYinm96y9H5eKXIJN88pLMxU7wlaCREzKtqV61V447C4P6BkLllOFGs20oawZSRcxgQwn6h5b8rh54gVrAu0Ck5MG+QeqkzFa31tY3I/ifrX9SM+SEcJCC1YBRMOV82pgSNA9hFpyTjqKoZDxYQuklC8ORG5zPrUXaKlYeEbz6TqkQWpYdJ0etCipwFWQpq/uuYWZ+7RZYIYvivZ3aM8ojeV8W7l1RCfcNkZeaxeQvHI3UG7BlovCz4z04Ww12TpIO1tf5bc6gTSjC918cRv0MNkFzwWFesYq29DotmP3rTi7HW50pDoiFbMnIK2OQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ys+CVKcfPX0WFeALBHE2JIx/MrGyjSkzcxtujfnpZq0=; b=kv94O5sjefcL5NorDcwFczY/EW0bRNTRwk9CYdzwv2DTM3m60bjEFSNrTVY+39RGfDZDiWIXfbs46nqTElgPjLleEikxr0ZKFBB6x39L8gN34ieoXvmJsGQRFuUeC0nKwI5ay7cRpdAXuQWwxgeuswtS9ghxLh6Ql6X92wzW7Ng= Received: from MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) by CY8PR10MB6442.namprd10.prod.outlook.com (2603:10b6:930:62::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Tue, 13 Dec 2022 18:44:57 +0000 Received: from MN2PR10MB3213.namprd10.prod.outlook.com ([fe80::dd41:a422:5763:8848]) by MN2PR10MB3213.namprd10.prod.outlook.com ([fe80::dd41:a422:5763:8848%7]) with mapi id 15.20.5880.019; Tue, 13 Dec 2022 18:44:57 +0000 From: David Faust To: gcc-patches@gcc.gnu.org Cc: indu.bhagat@oracle.com Subject: [PATCH v2 2/3] btf: fix 'extern const void' variables [PR106773] Date: Tue, 13 Dec 2022 10:44:25 -0800 Message-Id: <20221213184426.8861-3-david.faust@oracle.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221213184426.8861-1-david.faust@oracle.com> References: <20221213184426.8861-1-david.faust@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BYAPR01CA0063.prod.exchangelabs.com (2603:10b6:a03:94::40) To MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB3213:EE_|CY8PR10MB6442:EE_ X-MS-Office365-Filtering-Correlation-Id: d090a471-97e2-499d-953e-08dadd3a21fd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eT7yNpZbs40XyqBCFDHd1OSrukO7skVlT8EZEZiTDMw58XRKQIHccNNiG/nR55jvQsUcrIn3koreMMgGEcOjqJGrZSS0wYL0Iz61yVwtRkCG3aH2zvDqYqh/lgbBy8zGcsybsP3jX3Kyd9m/NED7sJbKP1KPyvdlxkMhO5wKLON3oQUCoIDUXlw6lUU/H3vAhWV+E9jgDYKtA57Zb2u6dr4Amlz7ALBupRq4hiUWs7P/QqU8x92wBSHmCgQkbHOiRDyDCdOr5a1dnYiV/C1lE93N2v/NIDHgPgmGbW93Gvi5fXmQwauMTAMPOCF9P97WCnERb7wOx6oy91k+pAJYEnASTXMPeq3M8fKc5dSerWpoDnRzv4Paw7cfH1qd1iLf2tDXtI8OuxG/xiCTfnZ9Rf3j8rIXXuUFSpYWKmBVEDoced93m1rzm0rEqx4jHCOYnfqqGeLPKPlDYqBg6e/NXx3+pLqSke2VvuIKySDaLl0KofTvu7C3DjebAHJe2mnT6I7uiktLCl6OT1AR2+nw/t9LKjgJuloNzXnMApdl8rwXLIIJPf1+N4I6VNashn3DVm0NXRrv5wlp8pZJ3LF7gkBkewLFgKTu8jqT2jGLiWPii4r4DYHdYTsg1hQidqn6bAwq5/dWmB79k/kBb23X4+FBzu/sunT1pTKHccUKIq4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN2PR10MB3213.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(396003)(39860400002)(366004)(346002)(136003)(376002)(451199015)(83380400001)(66476007)(41300700001)(86362001)(8936002)(38100700002)(66556008)(4326008)(5660300002)(66946007)(8676002)(44832011)(316002)(2906002)(107886003)(186003)(26005)(6506007)(6916009)(1076003)(6666004)(2616005)(6512007)(6486002)(478600001)(36756003)(84970400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?cGRZ8x8g36gvyMsOGbY3EndgxL13zM7mBa/BjbWt1U4oMswG93mOCqeYQ6sf?= =?us-ascii?Q?wXzKsz3QeIIYxM3xL/pnRt+0oCO5YqP9DUDp65Q2QJxUL6FSPRFpUmlZYf3F?= =?us-ascii?Q?a0j7q3aFtN97rRKstGb28xroqH30fZCRjevIGCcFr5tozMVY9AA3cLPmqUqd?= =?us-ascii?Q?H+KstBGYCWg5e3yxJz0tMJL5Tt/Em7LHqIyU+AFIdS+zTJ3QB+RCQ3LPl8cm?= =?us-ascii?Q?uiHPKHHnbZQv4/scKHGcyQffem1hnX+0DpIgIpWsXuez+p6H42G54ON7jU3W?= =?us-ascii?Q?71TpQ1WCoAIQuz1fVXMvz8qcc+5XiOKDOh95pTqQLZV3l61Ih8F+2/2CdnUm?= =?us-ascii?Q?CaVytCMy6Ek0Quo1p90LhofLJQMLarJZY059YjRf/nMTFWhx4xxRzbY/h1px?= =?us-ascii?Q?tXcmqJIB8utMy8lIgU3TUp4+DVdQaAZzMuDp/sLWUFDwzZHWsDxenV3OODrq?= =?us-ascii?Q?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tMI2ig?= =?us-ascii?Q?ANdjmyXwRuA38zp2Etb1R5n/HK0l1eYsY47kEaS//MDK21uTgSPY/i2fFCKP?= =?us-ascii?Q?/zXkUbjCtqGgx6/gdOJHdUucWWL3ZLiOYQK2zSyZkxb97zlMfGid7pI1QX/s?= =?us-ascii?Q?0sj4o8a2RGWizwp3Mni2SJ8vfITADv308Z+aqtqou24BMsXHOAOo7yBvS5Y6?= =?us-ascii?Q?QFYXpEGlWiAwJNxP7I2ql1d2t1Ci0/TivHQGrtNm?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d090a471-97e2-499d-953e-08dadd3a21fd X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3213.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Dec 2022 18:44:57.7267 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: HJgUkmLvVixsnKz+sn9rMb0Wc5A0OfF4l9A9Gpq6EIiR9CNSfhFBhSrWUeIezWdj83OD0mx7USgiOTPsONrsRQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR10MB6442 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-12-13_03,2022-12-13_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 spamscore=0 bulkscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 mlxscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212130164 X-Proofpoint-ORIG-GUID: -lV652P4KzYt9HjSE99-hwqN7fNo5AI7 X-Proofpoint-GUID: -lV652P4KzYt9HjSE99-hwqN7fNo5AI7 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP 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: [Changes from v1: Minor updates to comments per review. ] The eBPF loader expects to find BTF_KIND_VAR records for references to extern const void symbols. We were mistakenly identifing these as unsupported types, and as a result skipping emitting VAR records for them. In addition, the internal DWARF representation from which BTF is produced does not generate 'const' modifier DIEs for the void type, which meant in BTF the 'const' qualifier was dropped for 'extern const void' variables. This patch also adds support for generating a const void type in BTF to correct emission for these variables. PR target/106773 gcc/ * btfout.cc (btf_collect_datasec): Correct size of void entries. (btf_dvd_emit_preprocess_cb): Do not skip emitting variables which refer to void types. (btf_init_postprocess): Create 'const void' type record if needed and adjust variables to refer to it as appropriate. gcc/testsuite/ * gcc.dg/debug/btf/btf-pr106773.c: New test. --- gcc/btfout.cc | 44 +++++++++++++++++-- gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c | 25 +++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c diff --git a/gcc/btfout.cc b/gcc/btfout.cc index 677e8324424..204b11d4e9f 100644 --- a/gcc/btfout.cc +++ b/gcc/btfout.cc @@ -350,6 +350,8 @@ btf_collect_datasec (ctf_container_ref ctfc) tree size = DECL_SIZE_UNIT (node->decl); if (tree_fits_uhwi_p (size)) info.size = tree_to_uhwi (size); + else if (VOID_TYPE_P (TREE_TYPE (node->decl))) + info.size = 1; /* Offset is left as 0 at compile time, to be filled in by loaders such as libbpf. */ @@ -441,7 +443,7 @@ btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc) return 1; /* Do not add variables which refer to unsupported types. */ - if (btf_removed_type_p (var->dvd_type)) + if (!voids.contains (var->dvd_type) && btf_removed_type_p (var->dvd_type)) return 1; arg_ctfc->ctfc_vars_list[num_vars_added] = var; @@ -1075,15 +1077,49 @@ btf_init_postprocess (void) { ctf_container_ref tu_ctfc = ctf_get_tu_ctfc (); - size_t i; - size_t num_ctf_types = tu_ctfc->ctfc_types->elements (); - holes.create (0); voids.create (0); num_types_added = 0; num_types_created = 0; + /* Workaround for 'const void' variables. These variables are sometimes used + in eBPF programs to address kernel symbols. DWARF does not generate const + qualifier on void type, so we would incorrectly emit these variables + without the const qualifier. + Unfortunately we need the TREE node to know it was const, and we need + to create the const modifier type (if needed) now, before making the types + list. So we can't avoid iterating with FOR_EACH_VARIABLE here, and then + again when creating the DATASEC entries. */ + ctf_id_t constvoid_id = CTF_NULL_TYPEID; + varpool_node *var; + FOR_EACH_VARIABLE (var) + { + if (!var->decl) + continue; + + tree type = TREE_TYPE (var->decl); + if (type && VOID_TYPE_P (type) && TYPE_READONLY (type)) + { + dw_die_ref die = lookup_decl_die (var->decl); + if (die == NULL) + continue; + + ctf_dvdef_ref dvd = ctf_dvd_lookup (tu_ctfc, die); + if (dvd == NULL) + continue; + + /* Create the 'const' modifier type for void. */ + if (constvoid_id == CTF_NULL_TYPEID) + constvoid_id = ctf_add_reftype (tu_ctfc, CTF_ADD_ROOT, + dvd->dvd_type, CTF_K_CONST, NULL); + dvd->dvd_type = constvoid_id; + } + } + + size_t i; + size_t num_ctf_types = tu_ctfc->ctfc_types->elements (); + if (num_ctf_types) { init_btf_id_map (num_ctf_types + 1); diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c b/gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c new file mode 100644 index 00000000000..f90fa773a4b --- /dev/null +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c @@ -0,0 +1,25 @@ +/* Test BTF generation for extern const void symbols. + BTF_KIND_VAR records should be emitted for such symbols if they are used, + as well as a corresponding entry in the appropriate DATASEC record. */ + +/* { dg-do compile } */ +/* { dg-options "-O0 -gbtf -dA" } */ + +/* Expect 1 variable record only for foo, with 'extern' (2) linkage. */ +/* { dg-final { scan-assembler-times "\[\t \]0xe000000\[\t \]+\[^\n\]*btv_info" 1 } } */ +/* { dg-final { scan-assembler-times "\[\t \]0x2\[\t \]+\[^\n\]*btv_linkage" 1 } } */ + +/* { dg-final { scan-assembler-times "ascii \"foo.0\"\[\t \]+\[^\n\]*btf_string" 1 } } */ + +/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_offset" 1 } } */ +/* { dg-final { scan-assembler-times "1\[\t \]+\[^\n\]*bts_size" 1 } } */ + +extern const void foo __attribute__((weak)) __attribute__((section (".ksyms"))); +extern const void bar __attribute__((weak)) __attribute__((section (".ksyms"))); + +unsigned long func () { + unsigned long x = (unsigned long) &foo; + + return x; +} + -- 2.38.1