From patchwork Wed Dec 7 20:57:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Faust X-Patchwork-Id: 61679 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 C5A32390CE8D for ; Wed, 7 Dec 2022 20:58:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5A32390CE8D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670446738; bh=uomGS3HHPPEYME3VLhatSv1DlwNHuVcfwoiMB4SVST4=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=NmCr2PWSVghodkjx9evVSFzrskxTj/ANLjhRVVogQVD1ZWoOqNlxY6hL8D96uvI+Z LzPdjGY9oZf92NUvjXs3/eE7HiMK5IlXEoVpdhAyHFQ363jbUALExfQnmubgV00JAR /6pDDlu1edl+ceZLVPSNGGZAMit9typWQQpIx+z8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 6EB2138B6341 for ; Wed, 7 Dec 2022 20:57:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6EB2138B6341 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2B7J6CDS021939 for ; Wed, 7 Dec 2022 20:57:53 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3maubahe4u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 07 Dec 2022 20:57:53 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2B7J2Lh5021720 for ; Wed, 7 Dec 2022 20:57:52 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2107.outbound.protection.outlook.com [104.47.70.107]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3maa8gexs2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 07 Dec 2022 20:57:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Y6x8/c8QaoU5nteWPf9lljLBZhJKIkRD5rq6MJ6SnQ+EP1Fnn/+0W7lBvKvHVAn/PMuIOr4s1wGNVYDlzsRz1kWiamceUlQbStVJ+r7SfeQzBrEjC6iHDyxs1JRwI+1MfWwSXHHkhcB36xVSp3+BAAFi3/ddEAhXr+ZLcEUD/qau0JDsWCFxk1yFNH7YdDP1EMStETXPxDMsw+qhX6N5BFsoNk1Kb4zXihZOtMWgtF79Ba9KDBOdBrMnWvbV4GaqytR7HxEiiYULLaAwY/22V3y5HVNNpNAj7PK3nmxmEqy3vUU+M5H1VEeuxV9w6ioWL+Rpw8LW+MkrxIuFx1uXRA== 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=uomGS3HHPPEYME3VLhatSv1DlwNHuVcfwoiMB4SVST4=; b=GYQz3M/x1MRKq7j+4j9FVIBcoQ0gLeJ8Oro8rI8R+aljPYvh3aA3XI2tIEZOyVRtiP5cO2z1qfglm4s4spNADXvUiAdy7CsC/qTtxtBGGLs5W6m1s0FOCqJsr7MNxNpIIXhX/9OXIjKCZzsDhX2tNhezsdieatx/eI7hEjjAHmKGOzYvwMwXxEFEI36DzgHvDjze7fvyTYjmChtA6qLnr3dJ86tmkpkFzYiDHX87k+l9n3152o0la7Ux2qcU1TMCfYlnIUQsRm8jpT4/BNY3L/W94RJGXcpfYkmImlE4C0rne+adZsRv7l3EiQMxiLgMcoYVra/tfBNjTpk7X0XY0A== 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 Received: from MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) by DM6PR10MB4330.namprd10.prod.outlook.com (2603:10b6:5:21f::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Wed, 7 Dec 2022 20:57:50 +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.014; Wed, 7 Dec 2022 20:57:50 +0000 To: gcc-patches@gcc.gnu.org Cc: indu.bhagat@oracle.com, jose.marchesi@oracle.com Subject: [PATCH 1/3] btf: add 'extern' linkage for variables [PR106773] Date: Wed, 7 Dec 2022 12:57:32 -0800 Message-Id: <20221207205734.9287-2-david.faust@oracle.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221207205734.9287-1-david.faust@oracle.com> References: <20221207205734.9287-1-david.faust@oracle.com> X-ClientProxiedBy: SJ0PR05CA0112.namprd05.prod.outlook.com (2603:10b6:a03:334::27) To MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB3213:EE_|DM6PR10MB4330:EE_ X-MS-Office365-Filtering-Correlation-Id: 8d07e15f-7c86-437a-b6c9-08dad895b391 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JWyStInh1DCog0MrKmf25TNaARPl1K6h5mNqgbBiIb4M9J+4wZHM1CAcUNxTCwgTDHHZ6sXm8JE81mbMtmmFqoud0CGSmCw/DsXBt7rnqMq3Suno0QR8IGZ7cbjngKE0FfxN75dhOZxSQ15I5gnOqpYTZ95j1uHVlSvddLbSXzvz/YEzXetWbg7IogtLq/EFH3FRB8C4btFvEqSUGFJFxrr/JxVLK4acnxPIWzgJ+uWtTP85M6Cw5CpB6/l4ThA+E9VZmNuwQIxb/363iynusR95RRk+KE13ZIoThOjNbJmS2zJeLBW0PjrJbE/Mcnu8RWPMXpLxNu3CB50SZbvqQTBa8SDOstjAvClkU6iQm2fkwqgc/71efpuvK5+EcWys/Gq0COFWc12VRxxCDBq27mphUZK2Sigvn5eoKy2aXYjPu2Ryf0IkJc99y2bzkwD3DYMhm5jabXF+evjud3/aTw4MFQvw7PKTsgZtpXAjbQpItJuTHVHeoLnfVcpnQi2CJlmZ9cGuVlFeKFnDZKftCkZfMuP02ar5qBvEjopZzzAQZ4NTUNivyn3m0B60W0JJmW3YWvfuUs0PqCHh/gO8u+FfBAaIwXJC3seOLoXjj/vousy0f9hM+1D8u66mxIGCLosXZRgAssS/VeXRLVubxsEQ6JUd0pmNeBQATV0IQ+A= 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)(136003)(346002)(396003)(366004)(39860400002)(376002)(451199015)(41300700001)(86362001)(8936002)(478600001)(44832011)(2616005)(66476007)(66946007)(107886003)(6666004)(84970400001)(5660300002)(1076003)(66556008)(8676002)(38100700002)(4326008)(6486002)(6512007)(6506007)(83380400001)(26005)(186003)(316002)(36756003)(2906002)(6916009); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iWExoQGiDFLQsO4Z/WpAiTR3YQia9V2rAd+nULQOfoCbQJ3jz3ZcyXKUwXnEJvJj/0Uy2BGjza/s9pFyRsbzvGv7GYlRP5bWZO5vIiWd//vglbuAIsUlpW0gj3SGfO9uuei0K4UW5h6VLc1yKKm9deKt+UI3AmK58c74i5N1KiibSN74k4UCWYwwFXi1J7PoUP6kViEo7IhZaU4X3aJyB2IbiAV/pin5gyiU2IAw99GyPT2KODgE8jjHbXg8VkcITX3IOJ4Ud3kdxa4n75+z+K/Ea5BVB+mRWsT0wGS9VrEJFpBucaucumqcYslSLHEVg6i+dpOk3T696Y7cAvMHU3J4uxNW9YioHJ6Wdnso+/R4Ib9KMFR/n01pQxLRSBycnlH3su232Ccda89fNgNS3pw1ZMMOMdD+fRuHU6kb2bAFo5MxqNHjgFCUYh+vMqtMdtooI24TV0M23sAY79nkT1nirAust5JcHXlhA9rHgq7BlWPOMwbN8zPhQqfAL3QdJ+SB6siKEPAf5DXieRZ2u696uKSE3lj9iaReIowlQsxWrmjafrESQP1yF2IRv9MvHF32WvuMqc3fK5Q7867AQ0+24Vz8oEjxn+MBg8STSktVtsDWzYnMKG7FwjhMC/frSLg+WaoSk0gkwyYPMdTT2utDTVXiWnE8Coa6C8hMOj7lqOUSHPsitDR3uIIe1iegSpmEyhxRZ+8O/Nhs/RRScj1zuJiQZs+cwNtWz2/bQimd7jBhBTX0iNchFVNoTOHgi263tXT94mEpLN112iMpjy4arMRhjE9RTScgjqXTvQmuHerkfRo7LvCxz1BNXIF7whuqGhT4t1ImvvLDK1s4UAoll/fWqXs9xFa51+YABWhfXExyRkmeLQ8DEIal2i8CPSqzaGQ/tGlcIj2jvZXZK4spAeecL7U+cdMjtpXQWbxh03OMiHNrlEBiWGGh5qAwVZctZL5UnkNCcrdPoEEL4yNOKFUSX9joLi+85TLorY7NEdS5OgkqRHC4ZslNxkxdUTFpXrk/WO79jCJzCPHFon0Xq3b49D9CwN4YRqpVOa7v+MiK8B4hx7XLUTRy9PJuuLqP6PdBX6vYJD45aVmXAy671dd6fNerWuGq0nnUctyvwXRnSFV0YK7TKiQ1RagdH0w9wMOGOQDUkkIW6Z7Y9tip1itoQSk+N89e1vEh6r5AJGGp3rYCnZ/nlMZC91y+T6e7Ba+0yKSewbZjX5+cOGd9YnsPOCMBr1La5vGHL0/f3TA4+qCy/ciRqhLQqPNfKrGsQ+29w/wEp+jTEt9RkBhjG528EvKoAjluZiAy5AojgMV9cIx0Mayv4wGJl07W9kvyQbUUxSfYV9qMC8DP51puw+mR20ob9S3/cEROMqK/egIVbVq40JzUqVhG4egXiKhN64sG4vK+SRlWSsVllJgnFQWCsAUYEcYtOjs5mJjy7IKD4yl3ncziNpFvcsARIeGE6N8umU6wBRCn0gARr/5mqdzy61JVXHdcHvboJFKYjyYZuaKt5ipNm7KXFkBdWqFebJfxUcoFfO2w9KJeIc6fxJM9mk/fZCZRocck2Wt7HpW5ZpVgXF6SJc+QuCfk X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d07e15f-7c86-437a-b6c9-08dad895b391 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3213.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Dec 2022 20:57:50.3556 (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: Vv8N9DnZui86bDoonnz2WBs3o+47Agja/7Cy2wfAeDdXV9WkCerCsq/Y3Vtkn1+F/W1Xzm5G79kEevyRBTR9GA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4330 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-07_09,2022-12-07_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 bulkscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxscore=0 spamscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212070176 X-Proofpoint-GUID: lqV9nxJpZCRxD-sKzE1jk00mGlVg7qzL X-Proofpoint-ORIG-GUID: lqV9nxJpZCRxD-sKzE1jk00mGlVg7qzL X-Spam-Status: No, score=-13.1 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Faust via Gcc-patches From: David Faust Reply-To: David Faust Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Add support for the 'extern' linkage value for BTF_KIND_VAR records, which is used for variables declared as extern in the source file. PR target/106773 gcc/ * btfout.cc (BTF_LINKAGE_STATIC): New define. (BTF_LINKAGE_GLOBAL): Likewise. (BTF_LINKAGE_EXTERN): Likewise. (btf_collect_datasec): Mark extern variables as such. (btf_asm_varent): Accomodate 'extern' linkage. gcc/testsuite/ * gcc.dg/debug/btf/btf-variables-4.c: New test. include/ * btf.h (struct btf_var): Update comment to note 'extern' linkage. --- gcc/btfout.cc | 9 ++++++- .../gcc.dg/debug/btf/btf-variables-4.c | 24 +++++++++++++++++++ include/btf.h | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c diff --git a/gcc/btfout.cc b/gcc/btfout.cc index aef9fd70a28..a1c6266a7db 100644 --- a/gcc/btfout.cc +++ b/gcc/btfout.cc @@ -66,6 +66,10 @@ static char btf_info_section_label[MAX_BTF_LABEL_BYTES]; #define BTF_INVALID_TYPEID 0xFFFFFFFF +#define BTF_LINKAGE_STATIC 0 +#define BTF_LINKAGE_GLOBAL 1 +#define BTF_LINKAGE_EXTERN 2 + /* Mapping of CTF variables to the IDs they will be assigned when they are converted to BTF_KIND_VAR type records. Strictly accounts for the index from the start of the variable type entries, does not include the number @@ -314,6 +318,9 @@ btf_collect_datasec (ctf_container_ref ctfc) continue; const char *section_name = node->get_section (); + /* Mark extern variables. */ + if (DECL_EXTERNAL (node->decl)) + dvd->dvd_visibility = BTF_LINKAGE_EXTERN; if (section_name == NULL) { @@ -676,7 +683,7 @@ btf_asm_varent (ctf_dvdef_ref var) dw2_asm_output_data (4, var->dvd_name_offset, "btv_name"); dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info"); dw2_asm_output_data (4, get_btf_id (var->dvd_type), "btv_type"); - dw2_asm_output_data (4, (var->dvd_visibility ? 1 : 0), "btv_linkage"); + dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage"); } /* Asm'out a member description following a BTF_KIND_STRUCT or diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c b/gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c new file mode 100644 index 00000000000..d77600bae1c --- /dev/null +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c @@ -0,0 +1,24 @@ +/* Test BTF generation for extern variables. */ + +/* { dg-do compile } */ +/* { dg-options "-O0 -gbtf -dA" } */ + +/* Expect 4 variables. */ +/* { dg-final { scan-assembler-times "\[\t \]0xe000000\[\t \]+\[^\n\]*btv_info" 4 } } */ + +/* 2 extern, 1 global, 1 static. */ +/* { dg-final { scan-assembler-times "\[\t \]0\[\t \]+\[^\n\]*btv_linkage" 1 } } */ +/* { dg-final { scan-assembler-times "\[\t \]0x1\[\t \]+\[^\n\]*btv_linkage" 1 } } */ +/* { dg-final { scan-assembler-times "\[\t \]0x2\[\t \]+\[^\n\]*btv_linkage" 2 } } */ + +extern int a; +extern const int b; +int c; +static const int d = 5; + +int foo (int x) +{ + c = a + b + x; + + return c + d; +} diff --git a/include/btf.h b/include/btf.h index eba67f9d599..9a757ce5bc9 100644 --- a/include/btf.h +++ b/include/btf.h @@ -182,7 +182,7 @@ struct btf_param information about the variable. */ struct btf_var { - uint32_t linkage; /* Currently only 0=static or 1=global. */ + uint32_t linkage; /* 0=static, 1=global, 2=extern. */ }; /* BTF_KIND_DATASEC is followed by VLEN struct btf_var_secinfo entries, From patchwork Wed Dec 7 20:57:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Faust X-Patchwork-Id: 61677 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 A74CC39484B2 for ; Wed, 7 Dec 2022 20:58:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A74CC39484B2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670446709; bh=QBCCK1RUBPydLGVbu5Q+FDNuGw0ImgCf2bVapaO/8TQ=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=HHIupq/XZjUF1k00wy2leG6Qt4dt6kH0SHyTMFjeR+xu9t7ZhzgMC63tPQznMqtZV SyU+ShRQU9/P5+sBoyBC2KdcuShyk2eP5GdBNIGsL9CzEugwhmgnYDP6JQV+pxh/Re XsipfXfH78wHBfhlhiecoRVXW3V7O6xkII4QBZhw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 2DF123899421 for ; Wed, 7 Dec 2022 20:57:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2DF123899421 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2B7GDZdu030072 for ; Wed, 7 Dec 2022 20:57:57 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3maudk9e1v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 07 Dec 2022 20:57:57 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2B7JB0Mg033806 for ; Wed, 7 Dec 2022 20:57:55 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2169.outbound.protection.outlook.com [104.47.55.169]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3maa7x8g46-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 07 Dec 2022 20:57:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZSpCG01r2jnD0+zfge9v6KbBQR+R/hULuDDqwJNLedQJUUCcua7L4YsmqQmruY+qOSFEn1Ks7Z2VHl7aHTflWoe0wRIA/DAL8qzSxFa+54S5LKm2dUuxy6bX70lb2LqGg6fwu5kWB7XDZXkzQ7xJS3D9AlsN8yPTl6QontBKWP+6hBZ1EyEccrcd9ALhyI6IlhB2GLmqD9soVbscWP/LjOOLaoeSFR2tR1kEqLTvx55yhwR6ZMACWxKusuG+O9XANaFgE+Hgk/yw3vH2fwBXWb5FrUX1nycXsLY9v6kBA0Rs+Ppamn0XZit3pecYPkh+Q1XfAQiV2EyKJjH6ZcWstg== 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=QBCCK1RUBPydLGVbu5Q+FDNuGw0ImgCf2bVapaO/8TQ=; b=TRMjRtBwdjKhtkL0RZQP9+Jq9G57TvDZPQJWfs4lXj1FX38MNI1OW5PtamFSR1YTTwovhIHPYqKOOc5sTHc5JcW6S77ZRKrxgfnxXyw9BUmV3xytogFvxl52YgwpZ/sJINLkQSc+qi6s3LeMQ+QEIeGBtM5lj841dseigToqp4vCvfq6ZZlSyyqopBf8LloYpTIvmcenJXDxR3jRDWVPu9o8VDewfWMF8JjUKomBaO9kyW+7ir8nCuIf8ma39d4GvakMZmWX0Fiqrciw4Zh2yfmLEgB19ZG9wxGjPLbY7Uy58B5DrdSsG1ukXPe1csnjtRKW35qaXYv/jKprVrN2Hg== 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 Received: from MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) by SA1PR10MB6493.namprd10.prod.outlook.com (2603:10b6:806:29c::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Wed, 7 Dec 2022 20:57:54 +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.014; Wed, 7 Dec 2022 20:57:54 +0000 To: gcc-patches@gcc.gnu.org Cc: indu.bhagat@oracle.com, jose.marchesi@oracle.com Subject: [PATCH 2/3] btf: fix 'extern const void' variables [PR106773] Date: Wed, 7 Dec 2022 12:57:33 -0800 Message-Id: <20221207205734.9287-3-david.faust@oracle.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221207205734.9287-1-david.faust@oracle.com> References: <20221207205734.9287-1-david.faust@oracle.com> X-ClientProxiedBy: BY3PR05CA0031.namprd05.prod.outlook.com (2603:10b6:a03:39b::6) To MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB3213:EE_|SA1PR10MB6493:EE_ X-MS-Office365-Filtering-Correlation-Id: f201d984-9707-4db8-4089-08dad895b5ef X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9qe0Yngpww7QsE4+Y0EpOoP9qByGHhZe4DwujQ7Mbqw9pTuSigQOZwqaTMBlfY1L0GOY7ZXxURsBJeV9speR9R2tXld8BmMdaAfIpdp01XYrQgkcIhahGcBI5Zr7WKyGmC0W5Ym3mV3vrkss/pBpacTFXThXkfJWlNYkklGhNqaWS0cNK1a60pTm9SY1iq9HqQSuLz2Yj5LNKpNn1IeVN9FOIjwMU4qodiJVzu35EhjVAM4+jcRfxUbGahDwkLeeUHjBUQmUW6r+xRb5aB4qXzoUfri/Ntdbubq41L/iNamWJdjP81hbQG+89t1qNIj9lrHNjdNTxhqS+8+tCXVIFy6tVaRRvAOeQqRX3h3wtXZltQz0Ln9apvvkG70dAdBxuEc4n+ooA7wO2riSLZ5dibuqLh/vtLltgxImpsBJoJeU2KuYMKjDmNsjMiGtLdSzsDtlq0+nKwTtKLUBhiTsPuCb1B12I28F7Wl+duTcNIGYw+eP6XIXKVVomyCDc/oTMwMhviQ4CYClXvFcanBKi8A7vlrgGxY2w8rbblYZopY4wAiM/5vxOVir5ZlVF1VOxOPyn4F4rx5av07n82inzgEQVwkvqKoooB3SEsh8FORoJmsNzl8/taZCoHfJdSwRpobtgsdJ9lM84YkQrJOqlZJLmd6lDhGU1hs5/44zWbQ= 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)(136003)(39860400002)(346002)(396003)(366004)(376002)(451199015)(4326008)(316002)(6916009)(8676002)(66556008)(66946007)(66476007)(6512007)(478600001)(38100700002)(36756003)(6486002)(107886003)(6666004)(6506007)(186003)(26005)(1076003)(2616005)(41300700001)(86362001)(5660300002)(8936002)(44832011)(2906002)(83380400001)(84970400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: F8plJBy/Ou+DJhGMLfQh/w1UDZk2ZBY9D/vflg7XUnlBYfjTvdeKDNItzHjSXkyjA2XsBZtr466fcnl0TSaKfD32WCXlp/DSCMFh4oZZAcjEnifeOEJtna9S5kzHUXhOZ9kIh3qSKsDHMDdz+En+ndf2xiW07t4V44LYXTrcMQJgo3qAMBCgSprWqUONA9LBLBZCEaeZD93aep1U0ENmR+s97A5f8/Q9OE6tidydCtNytX0Q6xdnMqV5T4pMffCRSrSMstEYT6QA5BQhNdD5Sxch7s2qqWVKRsuPwXV84Fsle40iS4HRJ0oXqvlpUI5zCwDHP08QxypGWQ38HPRn3B2zb+QeRISYPMySjo1ppM4rMZMeVOGj6TNqNsKnKnIMjWmAfBLO/QxavYSz2OHdvAp3pn6wXJ+XIv6GSTbZC15nWb49xAOLwF9WSrlqlgTTqnglBmb4GT7jGDUeHEcscKoV1Ji2qzzfVOwR0osVjal7dM7gC1mRQmTvJ87uXSGx8lT9qXwz2EmNyGFPGHulqbxmTRTr8hhxy/NV9CSK2DWPcSZ6x8gEeZzfKreutPITz2uo9xNy1e/TrV6duZDIV4jN3uSs87yZZIxLJgErhbVxwL+8WPq7T/X5NRgfutiZef1FroB3iqp4RJ+WWEdlcFdVXDicwUoItEQDnavRWO2vUfb8EColC97msuBgOQwgcUf8xk+8Nd92wBly//4yUfpHdkmJsLtpN7rPVSQujNETYMg6jo3VD+oq/uDALZ//rZEG+AmlRkn+uPOO+N5qBKlIfWe0f1JeP3w92mTTWp4+LINIhmMxcf//klChctv2B0eaom6nnZcKOyLsNSQKr8jkzuKI7swhfD1PTMFFl/KtQKFqG1sXg/T1EhChIlEhTTBzrOK6GJYX6pjeCuKGdRHzH14p+m3//NKJXdI+ppVcfrQXqJeuqXxQtLQDRBN0Ej6BwBHQNWV3kkNzIfKe8BsEHQqIgijjzPhETFgDCew58rWw5Cnb6JpKsadSkjyrURI6RFgeVT/2m8ZKbBVO8LfMqVjM3I5zuVH9WdHODpQgl5X3G6xHKaKxizzeav41nhY0ExAxVwv0p3IEPhcf/nxnVhjcaGpGWHztVc6GZg5PqI1m0YwjLXcWyE+buKK8H8u0bMYpVra8hHaeTanuVZ7z8DsmRzOfiIxdPHYgRISR3+Ly+SExOYiqOv2RcA9fPJ1Eb57D8SGc2YkOgbYXV0DZzOIWrM0R82G/t9/eVRSGBYAEU3gKv+lvpkz2ph89uGRK0CCgRDaemOlkV0ie7Na/lHWEsAJV9KhKzWlog/F5GYHyLvElT1OKuYdPXSyQ1yoDiWGUw8zN5znuiJ83KS7raUmeP6JO+C2QL3OKpSlA2B8XkwIkMC4P8ihDDdKFEMeLf3jF5MDQRKj5QGgDoEoO2NaKVkgsmTb54im/4BpcCl7F3JKtH9d5rKa46tBo0kNqalJAQ6H0k49sMWnECoWpUFns7CPiVs0fuNjai5UJXAS44BrZVRX4Yvq2nwf8AW0HSYxO+vEW9t65HbGVJWLCJbDek5arW76i9yPR3E/tfXfhmC83G6kt8oPPNK1j X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f201d984-9707-4db8-4089-08dad895b5ef X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3213.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Dec 2022 20:57:54.2147 (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: PgakTyk1fmv1nICSn+qyzrrvGC6LR+vC7hbu4CyOs1qEyHNXWil3iNb7Z2Vs7yvNQWxqZNzt6U0btTvv+oa/JA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB6493 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-07_09,2022-12-07_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 adultscore=0 suspectscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212070176 X-Proofpoint-GUID: gJYtmMoVZ51LkfV-QFh7-IoP0pZulwoG X-Proofpoint-ORIG-GUID: gJYtmMoVZ51LkfV-QFh7-IoP0pZulwoG X-Spam-Status: No, score=-13.1 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Faust via Gcc-patches From: David Faust Reply-To: David Faust Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 a1c6266a7db..05f3a3f9b6e 100644 --- a/gcc/btfout.cc +++ b/gcc/btfout.cc @@ -354,6 +354,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. */ @@ -439,7 +441,7 @@ btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc) ctf_dvdef_ref var = (ctf_dvdef_ref) * slot; /* 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; @@ -1073,15 +1075,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; +} + From patchwork Wed Dec 7 20:57:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Faust X-Patchwork-Id: 61678 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 CCAFB39217A4 for ; Wed, 7 Dec 2022 20:58:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCAFB39217A4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670446713; bh=xjreCkKBcZYiTVOQa1cXI2pH8wTPjIJdKzSOEKV2lXE=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=iXwymQPooBbbEeJTMHZbADVU3rZq5Bj29GyB6zgAzO0V8Rx48oFvil5kaGigYhTVJ p48TMaF3+YLqpZjCg0L1VG4qV03nVpdywBVqfAYfie/Zn5NxImGVVmYLP99ZrHlX5g leZQOju0b9WfpiBA4NxZT9tOUjVzWRrnOrthNd4g= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 67C99381EC97 for ; Wed, 7 Dec 2022 20:58:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 67C99381EC97 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2B7GDnXh010592 for ; Wed, 7 Dec 2022 20:58:00 GMT Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3mauf8hc95-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 07 Dec 2022 20:58: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 2B7J7YPj017189 for ; Wed, 7 Dec 2022 20:58:00 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2174.outbound.protection.outlook.com [104.47.55.174]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3maa5yg674-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 07 Dec 2022 20:58:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gZ3F5Y1hSlU+OWHnkE9wllwbYzQRh6vDzulRddTT6PZ2V7V/YnyL9YeDssvxATe4NH0guy0CMzzEmgfS0f1jyjOC+NtmvRPaP3J/B/ykLrCGRG7c3jzaW43wBvGLQy573yXvLoNbsug+VSRBrQzLhbIX/MuNxHYbLUNwX+kSaBlVQZAyGfzsRNY0o6NEbc4050U/XbZ8GFxUWle799sAaDGEZQoml0NQHqvhdY0aZuLDG++KjZFFdm4PvOem6I6yJBDmKirVdfzc4KOx3x2086eYlGjRVdAGmvCi2YRYblK/kSXilznhKjnsIdWbQiISGBD3qq/7BJAXstVDANIliA== 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=xjreCkKBcZYiTVOQa1cXI2pH8wTPjIJdKzSOEKV2lXE=; b=I8O4vdJu9YCvKsv5x2exDCEyIAiog43dz4cvaHa8re0fHXyRaR00iVdonHyBqY/RnsTYBpbJRAGIRPCWATgQWiN38/FxXI5EM9h8fGgUFlan+0f1gsE0vhsAeUqvgSBj+JboYCSdwKR5wRuAX0ZR4p+VTmlgIqO+LGHCvfE/IyapFKdIozcbZDQvC4wC+7klh/AoAsAEGXpBmcR/Mc6fjcd5Gde9EHwykBcoQvSw0lay5KksAd2pwZkM6rKNZYCw3jQ7tNOnRAsH8sR+0M/2eMBH6s2nM/32vmNC9uFD3g75O46cy6QWWP3+tLpHL/E3TqjfgcnVQuFuqBWp6eDnaw== 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 Received: from MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) by SA1PR10MB6493.namprd10.prod.outlook.com (2603:10b6:806:29c::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Wed, 7 Dec 2022 20:57:58 +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.014; Wed, 7 Dec 2022 20:57:58 +0000 To: gcc-patches@gcc.gnu.org Cc: indu.bhagat@oracle.com, jose.marchesi@oracle.com Subject: [PATCH 3/3] btf: correct generation for extern funcs [PR106773] Date: Wed, 7 Dec 2022 12:57:34 -0800 Message-Id: <20221207205734.9287-4-david.faust@oracle.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221207205734.9287-1-david.faust@oracle.com> References: <20221207205734.9287-1-david.faust@oracle.com> X-ClientProxiedBy: SJ0PR13CA0076.namprd13.prod.outlook.com (2603:10b6:a03:2c4::21) To MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB3213:EE_|SA1PR10MB6493:EE_ X-MS-Office365-Filtering-Correlation-Id: c9691ddb-d76b-4fdb-8cd7-08dad895b870 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2aQmMska6jnJJrNIiXtj2YRo9ImS3UHXvlGo0Q7o5R1G2fcU54h13bG7T9JSorSeyWSQHcieD5dS6z9i3digEW+xH/62DqZBi90+5OQ3ra70zf96tP2UIfR1R3MIYzOq/elyDduZRqlbszshBnHgNIpLICtESpkQK3bHwul6LqyQ1UfTHFtoNt6/tE3lEsBrFTecdAFfXi+U2w/hIy39vCuPMCMINmkBVf/bZAp/6OExz5W63hINToyGMoz4mh8MCmpCCq49kHn5otblqQfaHDD1zun6Dm2cMalWSKe6JvbZea2ftZwsKXP9KkB2UqE7R6B2vNaAPz1+9HouM5btdUT67HQhI35y2jkELWD3MlSfg6Ihklu/QQBV1a8UeYBRd7s3DEq26XY5loIsHSPPOKvdGPt6KJ/+jQmyT9vpDH3Ou+FAKc/bQN0VrKZPjUn9xfYwlTZxPYr/lvNEsriU4jyr0X3h8iIMcpOZFJwTPQuAA7YwHRrPfPh5uQ1dQq+bywoVWjRN65tfy8ffKoz5O/Xv0Ew1IFn33ILBJpYIOeWk/X/dyQwBh0bBw726+mDv8bd2MQSxoYx/ciwHG7MFx5SkYRRrZhkK2LJVo56xd7PMQRxYV3jY1lhKHOO2DH0O74yN1OF/JAj2x3kQtZUDzX+wkeXC9GXu+h94V+cyAl86ttDRYHFpyWj9wvqkw5KJgFH1FSa4NhEgfpq0EMMsCQ== 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)(136003)(39860400002)(346002)(396003)(366004)(376002)(451199015)(4326008)(316002)(6916009)(8676002)(66556008)(66946007)(66476007)(6512007)(478600001)(38100700002)(36756003)(6486002)(107886003)(6666004)(6506007)(186003)(26005)(1076003)(2616005)(41300700001)(86362001)(5660300002)(8936002)(44832011)(2906002)(83380400001)(84970400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CvF6gys05Pgry/keNfb4sg8DNSi7WblqRA/VhS6iOA52dPrjhz2HQ1gyawuvO8UgZwXxASIA8w1TuLoNDbwf4GLA+/APlLQaaN0cP3IRu21kKBaxgVP2R9LYHxovZ1fs49BwRm80TBH+X1cbHFFRFeNGtWIn2Z6Kh206AU/rmtk84565mOs+uqiXKnd5CK1mzNd0foHBcGd6HsXazcsE7NCxA7LK1D1Eg/4fNuNV3EegTnqn5m1MMChpmscj8LnfT6yQjQVtdgFL1lD4/ZbAGsyyMEEyEOI0dyp7tWKRUt6BMab0BTxr4qySkCPxi5eTK2PONckZQm8m8xfQz9AjMgSOJW4SiwqE0vVVwOsRtBdj9n3MJkVps9boffKEjkMBRUcgp/CrK4A7sSxqkzeXqF4VnKGP+p3J5Btf4NiWol2XS66gRNItV160HeigBlVWtz2TfX9Nc17dsslL8A52lL8ffSq8p42dsy5/a5TsE0NT0dRsgKNK9t5IaZZ4q+eq/wFGvvQVpnHdytXz9Z9T4dmEBGsOIjc+wQAD3rvMxA9hFr1xXa546nUQy9sbwUFc+G06CXwD1zGqwBb0b56AuCDkSUsjtCR571bngQcBTaotj5aDj8sudl5pa4CVs8vh27624Fcas4kdnRg/TTU0f7Rp8k4oHlpIdIVsjG38ihJQ4vO63fqzRcG1ih3m06NSDtE4H9iyuu4MFha+MczCIwh/sO5iOBMxZ166ILMlCHeaS62u8RoXjRZVxSMEqA9Cl47uehnsktfJ+ItFD5uXAvywLY79iGh8scXEyTPp/uEOyqrxzkx7tT5rLRPWOjtGSzgp+H26nMtwajuZ3GaoZspACn6qPKwQ/dm+8gLiG3YqdKBTzaaU7h7r+DojsHt5VNOIB41T9wv6KR8cl6ibLns649ntdYaPRanLxV8MGxOrkMAYokf27tDiUMLrUp54jVS6Ui0Vg1sN+rBYNdr4XUF1D1p4FFH4+oDIWxxdDy5w+g8BCSFNi/Xflc7JVC529EjRaIqHGq4da9ZfQkhedPcZBUUpogADmhSO0nIhce401R6g8AudoPCum0qZNb1IY3VeG4vyka/lHNGerAL+69Sq25V/iceR5jOyGnUP2ohNxiJ8uv4NeF5owUmq6cs6vTjGfH3e11yUWq+JbrYsbnp7Dsuvv1dhy1vPBfSLgWMClOrO6gd6qaROzRaB6AnwBPdvTUCGGWl9vdCd6BfphFaUebODmuK6gt+wYLKvhsI3+OqBKILADOmENF5pu6aN1d52vxjghZdtK6e+VoKfa2kHtIhVfvxF3GDz/r3Bv/DIMH7B/EG6+I76zFVtnA9RXFkawP1C6Uy51rVfUN65LMTTCiILxo6w0dznVjumxkfIyN7drIZLzhIHbEOroEGzrDNGrR4Sl+2depVy0NHoQctj1vLdEA9I3zETTXlkazmehnFSbf9W6ScGyjS4DzVfANWvgUW+JBxREpvF22GodgVtD7M/z+zZYsKgTaD96Moz15lYQpMhZcznGXCbPHtDNsONeI9WXR7aTwo8lcc+K0goc0fOX7l6MJO9/llnVjWV1/I7Vlxddcwfji4dP1qo X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c9691ddb-d76b-4fdb-8cd7-08dad895b870 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3213.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Dec 2022 20:57:58.4956 (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: kM6QiAMwqnTOGdcqN+6gjLWkHzsh1MuXdixU7HxBqwZxwS+kamZrQ07qM4vtB6CYwRYiOjm0f5H74+q+yXQZsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB6493 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-07_09,2022-12-07_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 phishscore=0 spamscore=0 bulkscore=0 mlxscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212070176 X-Proofpoint-ORIG-GUID: X1mVtAIeFZGfsHCforCUp7SsDpdAvwG2 X-Proofpoint-GUID: X1mVtAIeFZGfsHCforCUp7SsDpdAvwG2 X-Spam-Status: No, score=-13.1 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Faust via Gcc-patches From: David Faust Reply-To: David Faust Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The eBPF loader expects to find entries for functions declared as extern in the corresponding BTF_KIND_DATASEC record, but we were not generating these entries. This patch adds support for the 'extern' linkage of function types in BTF, and creates entries for for them BTF_KIND_DATASEC records as needed. PR target/106773 gcc/ * btfout.cc (get_section_name): New function. (btf_collect_datasec): Use it here. Process functions, marking them 'extern' and generating DATASEC entries for them as appropriate. Move creation of BTF_KIND_FUNC records to here... (btf_dtd_emit_preprocess_cb): ... from here. gcc/testsuite/ * gcc.dg/debug/btf/btf-datasec-2.c: New test. * gcc.dg/debug/btf/btf-function-6.c: New test. include/ * btf.h (struct btf_var_secinfo): Update comments with notes about extern functions. --- gcc/btfout.cc | 129 ++++++++++++------ .../gcc.dg/debug/btf/btf-datasec-2.c | 28 ++++ .../gcc.dg/debug/btf/btf-function-6.c | 19 +++ include/btf.h | 9 +- 4 files changed, 139 insertions(+), 46 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c diff --git a/gcc/btfout.cc b/gcc/btfout.cc index 05f3a3f9b6e..d7ead377ec5 100644 --- a/gcc/btfout.cc +++ b/gcc/btfout.cc @@ -294,7 +294,35 @@ btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname, ds.entries.safe_push (info); datasecs.safe_push (ds); - num_types_created++; +} + + +/* Return the section name, as of interest to btf_collect_datasec, for the + given symtab node. Note that this deliberately returns NULL for objects + which do not go in a section btf_collect_datasec cares about. */ +static const char * +get_section_name (symtab_node *node) +{ + const char *section_name = node->get_section (); + + if (section_name == NULL) + { + switch (categorize_decl_for_section (node->decl, 0)) + { + case SECCAT_BSS: + section_name = ".bss"; + break; + case SECCAT_DATA: + section_name = ".data"; + break; + case SECCAT_RODATA: + section_name = ".rodata"; + break; + default:; + } + } + + return section_name; } /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created @@ -305,7 +333,60 @@ btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname, static void btf_collect_datasec (ctf_container_ref ctfc) { - /* See cgraph.h struct symtab_node, which varpool_node extends. */ + cgraph_node *func; + FOR_EACH_FUNCTION (func) + { + dw_die_ref die = lookup_decl_die (func->decl); + if (die == NULL) + continue; + + ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die); + if (dtd == NULL) + continue; + + /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and + also a BTF_KIND_FUNC. But the CTF container only allocates one + type per function, which matches closely with BTF_KIND_FUNC_PROTO. + For each such function, also allocate a BTF_KIND_FUNC entry. + These will be output later. */ + ctf_dtdef_ref func_dtd = ggc_cleared_alloc (); + func_dtd->dtd_data = dtd->dtd_data; + func_dtd->dtd_data.ctti_type = dtd->dtd_type; + func_dtd->linkage = dtd->linkage; + func_dtd->dtd_type = num_types_added + num_types_created; + + /* Only the BTF_KIND_FUNC type actually references the name. The + BTF_KIND_FUNC_PROTO is always anonymous. */ + dtd->dtd_data.ctti_name = 0; + + vec_safe_push (funcs, func_dtd); + num_types_created++; + + /* Mark any 'extern' funcs and add DATASEC entries for them. */ + if (DECL_EXTERNAL (func->decl)) + { + func_dtd->linkage = BTF_LINKAGE_EXTERN; + + const char *section_name = get_section_name (func); + /* Note: get_section_name () returns NULL for functions in text + section. This is intentional, since we do not want to generate + DATASEC entries for them. */ + if (section_name == NULL) + continue; + + struct btf_var_secinfo info; + + /* +1 for the sentinel type not in the types map. */ + info.type = func_dtd->dtd_type + 1; + + /* Both zero at compile time. */ + info.size = 0; + info.offset = 0; + + btf_datasec_push_entry (ctfc, section_name, info); + } + } + varpool_node *node; FOR_EACH_VARIABLE (node) { @@ -317,28 +398,13 @@ btf_collect_datasec (ctf_container_ref ctfc) if (dvd == NULL) continue; - const char *section_name = node->get_section (); /* Mark extern variables. */ if (DECL_EXTERNAL (node->decl)) dvd->dvd_visibility = BTF_LINKAGE_EXTERN; + const char *section_name = get_section_name (node); if (section_name == NULL) - { - switch (categorize_decl_for_section (node->decl, 0)) - { - case SECCAT_BSS: - section_name = ".bss"; - break; - case SECCAT_DATA: - section_name = ".data"; - break; - case SECCAT_RODATA: - section_name = ".rodata"; - break; - default: - continue; - } - } + continue; struct btf_var_secinfo info; @@ -363,6 +429,8 @@ btf_collect_datasec (ctf_container_ref ctfc) btf_datasec_push_entry (ctfc, section_name, info); } + + num_types_created += datasecs.length (); } /* Return true if the type ID is that of a type which will not be emitted (for @@ -461,29 +529,6 @@ btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, ctf_dtdef_ref dtd) if (!btf_emit_id_p (dtd->dtd_type)) return; - uint32_t btf_kind - = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info)); - - if (btf_kind == BTF_KIND_FUNC_PROTO) - { - /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and - also a BTF_KIND_FUNC. But the CTF container only allocates one - type per function, which matches closely with BTF_KIND_FUNC_PROTO. - For each such function, also allocate a BTF_KIND_FUNC entry. - These will be output later. */ - ctf_dtdef_ref func_dtd = ggc_cleared_alloc (); - func_dtd->dtd_data = dtd->dtd_data; - func_dtd->dtd_data.ctti_type = dtd->dtd_type; - func_dtd->linkage = dtd->linkage; - - vec_safe_push (funcs, func_dtd); - num_types_created++; - - /* Only the BTF_KIND_FUNC type actually references the name. The - BTF_KIND_FUNC_PROTO is always anonymous. */ - dtd->dtd_data.ctti_name = 0; - } - ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd); } diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c new file mode 100644 index 00000000000..f4b298cf019 --- /dev/null +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c @@ -0,0 +1,28 @@ +/* Test BTF generation of DATASEC records for extern functions. + + Only functions declared extern should have entries in DATASEC records. */ + +/* { dg-do compile } */ +/* { dg-options "-O0 -gbtf -dA" } */ + +/* Expect one DATASEC with vlen=1 (.foo_sec) and one with vlen=2 (.bar_sec) */ +/* { dg-final { scan-assembler-times "0xf000002\[\t \]+\[^\n\]*btt_info" 1 } } */ +/* { dg-final { scan-assembler-times "0xf000001\[\t \]+\[^\n\]*btt_info" 1 } } */ + +/* Function entries should have offset and size of 0 at compile time. */ +/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_offset" 3 } } */ +/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_size" 3 } } */ + +extern int foo (int a) __attribute__((section(".foo_sec"))); + + +extern int bar (int b) __attribute__((section(".bar_sec"))); +extern void chacha (void) __attribute__((section(".bar_sec"))); + +__attribute__((section(".foo_sec"))) +void baz (int *x) +{ + chacha (); + + *x = foo (bar (*x)); +} diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c b/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c new file mode 100644 index 00000000000..48a946ab14b --- /dev/null +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c @@ -0,0 +1,19 @@ +/* Test BTF extern linkage for functions. + + We expect to see one BTF_KIND_FUNC type with global linkage (foo), and + one BTF_KIND_FUNC type with extern linkage (extfunc). */ + +/* { dg-do compile } */ +/* { dg-options "-O0 -gbtf -dA" } */ + +/* { dg-final { scan-assembler-times "btt_info: kind=12, kflag=0, linkage=2" 1 } } */ +/* { dg-final { scan-assembler-times "btt_info: kind=12, kflag=0, linkage=1" 1 } } */ + +extern int extfunc(int a, int b); + +int foo (int x) { + + int y = extfunc (x, x+1); + + return y; +} diff --git a/include/btf.h b/include/btf.h index 9a757ce5bc9..f41ea94b75f 100644 --- a/include/btf.h +++ b/include/btf.h @@ -186,12 +186,13 @@ struct btf_var }; /* BTF_KIND_DATASEC is followed by VLEN struct btf_var_secinfo entries, - which describe all BTF_KIND_VAR types contained in the section. */ + which describe all BTF_KIND_VAR or extern BTF_KIND_FUNC types contained + in the section. */ struct btf_var_secinfo { - uint32_t type; /* Type of variable. */ - uint32_t offset; /* In-section offset of variable (in bytes). */ - uint32_t size; /* Size (in bytes) of variable. */ + uint32_t type; /* Type of BTF_KIND_VAR or BTF_KIND_FUNC item. */ + uint32_t offset; /* In-section offset (in bytes) of item. */ + uint32_t size; /* Size (in bytes) of item. */ }; /* BTF_KIND_ENUM64 is followed by VLEN struct btf_enum64 entries,