From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id CA9423857B95 for ; Fri, 17 Nov 2023 19:29:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA9423857B95 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CA9423857B95 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1700249394; cv=pass; b=t6n7rOmWhCDBQ09YkY5eMhPk/58CiubHSn3jxThWquoXFrt//oYtG3eTyHJVu6OuaK4LTnayoGEV3v/8+tJlEkGfn0gLz7DAqPfny0rz6WcGOLTYZmm5mZAhBexPmQJ4mA9eDPYtq6BSCG/PJxff1wANS5gpXA2jUD219rfVTjU= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1700249394; c=relaxed/simple; bh=+B8m5+1sTtZf8b8adJcdWcWrcppFhcRu4QxsC2FK5D8=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=iDhMouo/ccQq2cUw/BWSBaTWfbyNDC7rPMDMALX7VFbnfqtulCyr0caTMZ34TP7s7aNWdfo+Z60f5N5dlb3sFdGLh8MWXnpofzGHiNunXvJfR3F1t4Gx1ATPNVU32/C0HWJkcOLA0/CykjLvqFGl9Q+yU89IEPcbJQ7Qp1Mzmiw= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3AHIQ3Jr030869; Fri, 17 Nov 2023 19:29:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : in-reply-to : references : date : message-id : content-type : mime-version; s=corp-2023-03-30; bh=idpDstTD5VCcK6tEPPDduEWTLJ+bM4Dc4OxenYwHrHA=; b=ob9Dc4nkq0KxLKgoLmwJ5HO97QMVz4/MDfD+OUZuq0knnK8uiYB5Uq+7elNqJbsezHYD gF5AE/22UkrgtgAtw/+WSNAU4VlpHbtJXizbsUXDTepPuGifS8a1in1KT1w5LZg7hF0c jho8hSJEXbppgOdFCLVjMKGJUNM0hJBRml/r7Uvjg0on5eiHTA/4kZD24KSj03qvlbjc 3TM7EmcZPsMsYdfnqqjAZatB6ZUxVnMwEgkFpn27wQe+ceMGffqZvGw9PT9Kmpg4mKeQ Us63A+U0FxPwYBFRR/XklJ+T+VMIZMF7Ok9cDD30FzkLAYFxMZlqZN+hTIj3oH+RIyDF DQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ua2m2p4ee-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 17 Nov 2023 19:29:50 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 3AHIo8MI015687; Fri, 17 Nov 2023 19:29:49 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2041.outbound.protection.outlook.com [104.47.74.41]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3uaxq2rp9h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 17 Nov 2023 19:29:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gmfrIp7EVyzt3KU/8+GpzImJkoYz6BpdzSVTRr7wZwIJhG0TdPqCmT3Bpwp9uZsGgP/XYQj3nKyraeQVL1KvVfrOEMsPEhjR2Jscnwn+iUxtkcJU+bjm71UWM0q344S3NFw1LT/ukKcYN6qf2MFl9LVr+CEQRKu8zwl3zHEhN6aYKwrivi2XMaqgFL/qDyt24C6nJ6XiDQvo1WxQfEoRq8gN0cXZi3FXsfzkguuq7IlNPZmhaQo//Je3wZjIeWyuvgYYJF1/cnhYEIL5p6a6tm0+gudT9IihuK1iv9/8HNNng2K1ReECE0ABJtyOCX8SP6sxkoThwL6v3gOyDT76Sg== 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=idpDstTD5VCcK6tEPPDduEWTLJ+bM4Dc4OxenYwHrHA=; b=HDXqSaCQytxkh/n+1z7CjoABYlsZ5SqFSXbG/GTJzUajhL72v18PBvGJpKxvZGPw+kQOQgt2/tOpQxQMCoPugQtguX0JgHHbtv/s7oB6bKBdOuvzxA2TNHW5RGgeRl6Ec9E73d5Hs4iIrskdgxR5z9zNOcqxggcR9hCgZlb6h7mvipoZrHPhdPPPyp4a4S7FapW2drrfetgbJpa3bjB+Yw+vufsILT0aXu8hzi/bXcgsf8Q7Ys/86WcGJNfrUQrQEeExZW8lyQfLCrp0OhmqkAOU1tIxurCReQ6iwBInAtDULa0nvAAjbZqifEXhLTO+TCGoR7SUcXdWb46UKru0zw== 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=idpDstTD5VCcK6tEPPDduEWTLJ+bM4Dc4OxenYwHrHA=; b=sYOjcWYYoy1FlnatlN1UIKkpqo+qn0+K/CuDzCFB39J97k3qgws0x0uFKTe1+j30WG4IPd4afD1yfLUePDvZJ3hksaibR/eyBV3Y/f9FGr4jbXZV6ymwFQwwkvJzzM3SNZ/37omAUcGxBzzAe+zk/Ybv49hCdr8S2Y7Jsi6w7k4= Received: from BYAPR10MB2888.namprd10.prod.outlook.com (2603:10b6:a03:88::32) by SN7PR10MB6452.namprd10.prod.outlook.com (2603:10b6:806:2a3::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.21; Fri, 17 Nov 2023 19:29:47 +0000 Received: from BYAPR10MB2888.namprd10.prod.outlook.com ([fe80::ba16:f585:1052:a61c]) by BYAPR10MB2888.namprd10.prod.outlook.com ([fe80::ba16:f585:1052:a61c%4]) with mapi id 15.20.7002.021; Fri, 17 Nov 2023 19:29:47 +0000 From: "Jose E. Marchesi" To: David Faust Cc: binutils@sourceware.org, jbeulich@suse.com Subject: Re: [PATCH v2] bpf: avoid creating wrong symbols while parsing In-Reply-To: <20231117185428.10823-1-david.faust@oracle.com> (David Faust's message of "Fri, 17 Nov 2023 10:54:28 -0800") References: <20231117185428.10823-1-david.faust@oracle.com> Date: Fri, 17 Nov 2023 20:29:44 +0100 Message-ID: <87edgo6uvr.fsf@oracle.com> User-Agent: Gnus/5.13 (Gnus v5.13) Content-Type: text/plain X-ClientProxiedBy: BLAP220CA0010.NAMP220.PROD.OUTLOOK.COM (2603:10b6:208:32c::15) To BYAPR10MB2888.namprd10.prod.outlook.com (2603:10b6:a03:88::32) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB2888:EE_|SN7PR10MB6452:EE_ X-MS-Office365-Filtering-Correlation-Id: 4d829154-f455-4334-7cd7-08dbe7a38f13 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nidiNmn0zvt62G5Hcc0VOErQj2ClGNlxRP+YXfjTKfKpwcUYdXiX0DgFk6Pn+I9yG4FGNKcm21grDZ96F58mikygPETYt23uOUX6iadHHgdv0y5BOJr+uGUxpljarqiuX2dNFfeEOX5s+83zvy+GphNfReHEe2vr4BbzigPB7UMICp2zPmoRHUviWMI++wg9wTn3+ggRn8wi6wBR6SntbDS4RGYiZdnHaQJyBsz5oejMIKuc6k08ICry1zOcpMtKto46kDVlWapCBTntSvV/K1oRYoIbLqd/mrgi7uVf0Kh4Wf8QEg5UL/t17Sb1+i73QPV8fAocyhNjOkDMYLRCN5rOlJBsIhfv4gxDXwkaUM0HNAFNJiIco+/xPfWLt4hgxj9ocaXQSK1lgNp6K4rNjcORFituC5vMLOJKqdEqVtAQkdVuf4/BcZVg3+qjxrduyB697rOo+BPo0AwiyaAR8qu8q8XcGdUShm2ZNskc308W9hpu7fD0n8c2daMvzEVfTZvn4kZTs92tPSM1N2nATlFLUTUsBZLVw0HeIzluXuM= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB2888.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(39860400002)(376002)(396003)(136003)(366004)(346002)(230922051799003)(451199024)(186009)(64100799003)(1800799009)(66946007)(66476007)(66556008)(41300700001)(37006003)(316002)(6636002)(4326008)(86362001)(5660300002)(2906002)(8676002)(6862004)(8936002)(83380400001)(38100700002)(6486002)(6666004)(966005)(478600001)(26005)(2616005)(6506007)(36756003)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Tv3GbasyL3JLMOAcEXb3ileqTGGsZYCpnNbS4kWT6kdfKnRkJHUVgzFDYJTf?= =?us-ascii?Q?GsV7ZSJgO1vCPkOpwSByurUWInEiMi2DHvRfDUf0VAdIF1XdT9pGTQwmxzQm?= =?us-ascii?Q?e+GaGmYoAKWIRmcNhJxPg/QVM605ud36dzVSzUWmMt7xfpMWisfzNd3GWMnd?= =?us-ascii?Q?cpq8VfjtnJHn0oFtXF6mp0AFQs6AH/eeOO27ecncJpd265nSif6qYkLmjdmG?= =?us-ascii?Q?h/WmSLxdaEaL+MMY1Sqoj1NqWI/Ysikud7Q02LSxoiRq/XfPRsQT94YCvsyc?= =?us-ascii?Q?Ej4vsvi2j2+HDOCjfYfIWCWgIMl8e8a2YA8QoB+gisPWfnUi87xhs6Ij+/Yp?= =?us-ascii?Q?txfxsnSEkT16amMwslEzcJf+w5limInFxDzg5cBz3rOa/bmPoCkmfE2GnM/w?= =?us-ascii?Q?EMvqrFLMLwc2qK7a8jrs3Qo63HciMM9KaO4N0nbqoqgq57pMPKj1GbvMkQ/m?= =?us-ascii?Q?fIRigv8YeGpoxOIJ4afM3z8R0fIhGTX8utCMmGN7pR/0Xc1HivihlOOuHnJY?= =?us-ascii?Q?jZj/6e020mcaO4NjGlQN9gp1METi+WoCY0Dc4ZXiLaYFaJ+1/RLqYNM9WLfO?= =?us-ascii?Q?tc4IMdSeJhbDhbBv1ZMyGAnPrxz3IBHVp64CxkQB4iDnHWc3WjwdVNaPibH0?= =?us-ascii?Q?qFvITlsaOmdd55j1lFNAi58IcnwOcWx7OTYjnSHjg6bHafE0IwpNHWci3EHp?= =?us-ascii?Q?1Zz61nuuyTDSZh9cmSsdfvd6YJGK5XC9cQIU5Wp59RBjdLfC17M7MbJt7Gr0?= =?us-ascii?Q?Qzy2W0hcr7aXrEOvef5ZT6aMt7PHZIjMzE3MP0zTp3U72C5ecVDlyRlfIiqc?= =?us-ascii?Q?p9iugKptcNvkfOA1eL0osOH6GjNSH5Uv6lW2umsFEoGPcTKldbv7KGnRNRIg?= =?us-ascii?Q?J68MIVaXokVtJN83QcTMw/1I7ojRY51z2Bmj+BaCOLlcYxfYq9p3BHljSURZ?= =?us-ascii?Q?rENnww3W9cMVyDs8w/Ofqvjw+JyEC2ymkAN2KddpXouVDIir53DDa+jK0Lsy?= =?us-ascii?Q?XKuBeS8K+UC1g0oAT25Lp+X1OkgIQBQeIOxy2vOI/dCirszbYPIaXh6IYG2e?= =?us-ascii?Q?H5HRZJNidazbkykEjJvGRZ8BMhEhBEkhblO6hmw5x91mOcW3Ir6cnY6pD77D?= =?us-ascii?Q?WcbUlSblV1PfqMTOeyzQG0QOhTtRNWryYeIHFRqnaSFDi9pgd+VCko34py+8?= =?us-ascii?Q?y6KNud/6z6jTUjkOLV4EB6lg7O0FWwhw+RD0mkuVuWHK6F87+yT9tZ/csf6k?= =?us-ascii?Q?UFSfsvCbGNalRxWcUWIfpXoQQ+fPhqCFdQ+83Ms2Tynkxuk42q2QlshMIcsI?= =?us-ascii?Q?ZQrofkMtgZao8XlWwYQ6nB8v4K4NSBgJNMLFRsZw+I4PAM7vJEeicW23+VwJ?= =?us-ascii?Q?cvkHJXhqcuwyTEICsUhSPTLr/XSal7gIVkky44M2XKHIvQHSNnQK7MfIXGFE?= =?us-ascii?Q?SzmGBQIZE8Kg3KAKfO3MssGl5MhLoiJsCd9QiyD7u2MihtZklvjRz82suXnB?= =?us-ascii?Q?Bl9vQeu9zGbdgzv2L4GZOZLww8uJJn9IfiZZFRuP1D3wDFEVzSb4lfNJrBQo?= =?us-ascii?Q?7lDCi0GY9g6Zovm59RTYKBI3/B/GsVE28slX2u/y3LzJpA18W0ookqhyssA6?= =?us-ascii?Q?WA=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: UoYfwOvCAuFJ6I+XFnLgw2dqCfI5ACA4Tf9A3PC+tefyhJL6smcheqe+CVu0JOPE9N61dXN12mAo9jE9g8sM5Z0rkttMjDy4FdesuR+Hntso2wiCBILJTdY9uGtZLJnV+Lfl/Z+dpUoaLyJ5T0yzAC4E8CivPnVrnpmGfedokTLo3bADXpNZHpxfgqh6ag/PE625EsMApqg91eUn0OO3NpWoiwcKpW4jE3cM5F7uW3IOUI7IXeFbRP7qw6O2h33GDJLI+HgiNfpbh753dPb7zsfcsjYUTuLv844U4VJ0qWSW/zrTFDf4RldPNUmEwWL7ZhalI++vm/dctVfIvGUlUdFPtmW0UABLrcSLu28z01gp9YjlafbqSd8N21bhRbAu0GGWqEO0wG3ZDn6rD3h3vfW37YATIAbQ4RVHSa3f2kkjYGa3QP3Fi6hEfFvF403CekZyhP4M5ECybCi7F75BR7syXoV7r5n88Mv8TPPylX4kT18gfVYclufJj3D86wUTvWGUmPF2JCGzJJqmlvvsveVCBQKUSBIKL+lIrBPydXfYdj1VmTFcDj4T2j58G3f5sO1+y2kGchKYKiTDuVn6nPiNnIKhycyDFfzBat7akQz62ElWxq7D/EkRxJedetUwNH2gMhHOgDfvv4nbRmWoK1iDd9XKO7lmEWMYOKNyKIwGILM1rbx9b4EQcx7lfYiBNzBTCbmpG+3g/ptqp7ATIBqV8gfGwELKrfNFUhOJO3BWAlCKfnH775/SGHjvdPRyrEKUTC1+kdquHqjfHvgryop3/5Bjk+3ddR8I9h/9aEs= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4d829154-f455-4334-7cd7-08dbe7a38f13 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB2888.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Nov 2023 19:29:47.1015 (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: klV33miKOmZsC5b+entapflpnxLh1VkCSBSDDKcivyd2JlX7AiKW3KDemMxRhoDe8Bq7NpDb9JI0Mq8M6G5ZznQOnKcWEct02/ICw+PseAY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR10MB6452 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-17_18,2023-11-17_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 adultscore=0 phishscore=0 mlxscore=0 bulkscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2311170148 X-Proofpoint-GUID: LAB56XGQoDohE48dJduRfsxpF4uCWO0g X-Proofpoint-ORIG-GUID: LAB56XGQoDohE48dJduRfsxpF4uCWO0g X-Spam-Status: No, score=-12.6 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_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: Hi David. > [ WAS: gas,bpf: cleanup bad symbols created while parsing > v1: https://sourceware.org/pipermail/binutils/2023-November/130556.html > > Changes from v1: > - Rewrite patch to avoid inserting wrong symbols in the first place, > rather than insert, remove, re-insert dance. > Suggested by Jan Beulich. ] > > To support the "pseudo-C" asm dialect in BPF, the BPF parser must often > attempt multiple different templates for a single instruction. In some > cases this can cause the parser to incorrectly parse part of the > instruction opcode as an expression, which leads to the creation of a > new undefined symbol. > > Once the parser recognizes the error, the expression is discarded and it > tries again with a new instruction template. However, symbols created > during the process are added to the symbol table and are not removed > even if the expression is discarded. > > This is a problem for BPF: generally the assembled object will be loaded > directly to the Linux kernel, without being linked. These erroneous > parser-created symbols are rejected by the kernel BPF loader, and the > entire object is refused. > > This patch remedies the issue by tentatively creating symbols while > parsing instruction operands, and storing them in a temporary list > rather than immediately inserting them into the symbol table. Later, > after the parser is sure that it has correctly parsed the instruction, > those symbols are committed to the real symbol table. > > This approach is modeled directly after Jan Beulich's patch for RISC-V: > > commit 7a29ee290307087e1749ce610207e93a15d0b78d > RISC-V: adjust logic to avoid register name symbols > > Many thanks to Jan for recognizing the problem as similar, and pointing > me to that patch. > > Tested on x86_64-linux-gnu host for bpf-unknown-none target. > > gas/ > > * config/tc-bpf.c (parsing_insn_operands): New. > (parse_expression): Set it here. > (deferred_sym_rootP, deferred_sym_lastP): New. > (orphan_sym_rootP, orphan_sym_lastP): New. > (bpf_parse_name): New. > (parse_error): Clear deferred symbol list on error. > (md_assemble): Clear parsing_insn_operands. Commit deferred > symbols to symbol table on successful parse. > * config/tc-bpf.h (md_parse_name): Define to... > (bpf_parse_name): ...this. New prototype. > * testsuite/gas/bpf/asm-extra-sym-1.s: New test source. > * testsuite/gas/bpf/asm-extra-sym-1.d: New test. > * testsuite/gas/bpf/bpf.exp: Run new test. > --- > gas/config/tc-bpf.c | 92 +++++++++++++++++++++++++ > gas/config/tc-bpf.h | 4 ++ > gas/testsuite/gas/bpf/asm-extra-sym-1.d | 7 ++ > gas/testsuite/gas/bpf/asm-extra-sym-1.s | 1 + > gas/testsuite/gas/bpf/bpf.exp | 3 + > 5 files changed, 107 insertions(+) > create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-1.d > create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-1.s > > diff --git a/gas/config/tc-bpf.c b/gas/config/tc-bpf.c > index fd4144a354b..3122f80804a 100644 > --- a/gas/config/tc-bpf.c > +++ b/gas/config/tc-bpf.c > @@ -1223,6 +1223,7 @@ add_relaxed_insn (struct bpf_insn *insn, expressionS *exp) > See md_operand below to see how exp_parse_failed is used. */ > > static int exp_parse_failed = 0; > +static bool parsing_insn_operands = false; > > static char * > parse_expression (char *s, expressionS *exp) > @@ -1230,6 +1231,9 @@ parse_expression (char *s, expressionS *exp) > char *saved_input_line_pointer = input_line_pointer; > char *saved_s = s; > > + /* Wake up bpf_parse_name before the call to expression (). */ > + parsing_insn_operands = true; > + > exp_parse_failed = 0; > input_line_pointer = s; > expression (exp); > @@ -1251,6 +1255,71 @@ parse_expression (char *s, expressionS *exp) > return s; > } > > +/* Symbols created by this parse, but not yet committed to the real > + symbol table. */ > +static symbolS *deferred_sym_rootP; > +static symbolS *deferred_sym_lastP; > + > +/* Symbols discarded by a previous parse. Symbols cannot easily be freed > + after creation, so try to recycle. */ > +static symbolS *orphan_sym_rootP; > +static symbolS *orphan_sym_lastP; > + > +/* Implement md_parse_name hook. Handles any symbol found in an expression. > + This allows us to tentatively create symbols, before we know for sure > + whether the parser is using the correct template for an instruction. > + If we end up keeping the instruction, the deferred symbols are committed > + to the real symbol table. This approach is modeled after the riscv port. */ > + > +bool > +bpf_parse_name (const char *name, expressionS *exp, enum expr_mode mode) > +{ > + symbolS *sym; > + > + /* If we aren't currently parsing an instruction, don't do anything. > + This prevents tampering with operands to directives. */ > + if (!parsing_insn_operands) > + return false; > + > + gas_assert (mode == expr_normal); > + > + if (symbol_find (name) != NULL) > + return false; Is this check correct? I see that expr.c:operand calls symbol_find_or_make right after calling md_parse_name (if the later is defined) and actually installs the resulting symbol in expressionP even if it was already defined. That is not done if md_parse_name returns `false'. > + > + for (sym = deferred_sym_rootP; sym; sym = symbol_next (sym)) > + if (strcmp (name, S_GET_NAME (sym)) == 0) > + break; > + > + /* Tentatively create a symbol. */ > + if (!sym) > + { > + /* See if we can reuse a symbol discarded by a previous parse. > + This may be quite common, for example when trying multiple templates > + for an instruction with the first reference to a valid symbol. */ > + for (sym = orphan_sym_rootP; sym; sym = symbol_next (sym)) > + if (strcmp (name, S_GET_NAME (sym)) == 0) > + { > + symbol_remove (sym, &orphan_sym_rootP, &orphan_sym_lastP); > + break; > + } > + > + if (!sym) > + sym = symbol_create (name, undefined_section, &zero_address_frag, 0); > + > + /* Add symbol to the deferred list. If we commit to the isntruction, > + then the symbol will be inserted into to the real symbol table at > + that point (in md_assemble). */ > + symbol_append (sym, deferred_sym_lastP, &deferred_sym_rootP, > + &deferred_sym_lastP); > + } > + > + exp->X_op = O_symbol; > + exp->X_add_symbol = sym; > + exp->X_add_number = 0; > + > + return true; > +} > + > /* Parse a BPF register name and return the corresponding register > number. Return NULL in case of parse error, or a pointer to the > first character in S that is not part of the register name. */ > @@ -1317,6 +1386,16 @@ parse_error (int length, const char *fmt, ...) > va_end (args); > partial_match_length = length; > } > + > + /* Discard deferred symbols from the failed parse. They may potentially > + be reused in the future from the orphan list. */ > + while (deferred_sym_rootP) > + { > + symbolS *sym = deferred_sym_rootP; > + symbol_remove (sym, &deferred_sym_rootP, &deferred_sym_lastP); > + symbol_append (sym, orphan_sym_lastP, &orphan_sym_rootP, > + &orphan_sym_lastP); > + } > } > > /* Assemble a machine instruction in STR and emit the frags/bytes it > @@ -1606,6 +1685,10 @@ md_assemble (char *str ATTRIBUTE_UNUSED) > } > } > > + /* Mark that we are no longer parsing an instruction, bpf_parse_name does > + not interfere with symbols in e.g. assembler directives. */ > + parsing_insn_operands = false; > + > if (opcode == NULL) > { > as_bad (_("unrecognized instruction `%s'"), str); > @@ -1622,6 +1705,15 @@ md_assemble (char *str ATTRIBUTE_UNUSED) > > #undef PARSE_ERROR > > + /* Commit any symbols created while parsing the instruction. */ > + while (deferred_sym_rootP) > + { > + symbolS *sym = deferred_sym_rootP; > + symbol_remove (sym, &deferred_sym_rootP, &deferred_sym_lastP); > + symbol_append (sym, symbol_lastP, &symbol_rootP, &symbol_lastP); > + symbol_table_insert (sym); > + } > + > /* Generate the frags and fixups for the parsed instruction. */ > if (do_relax && isa_spec >= BPF_V4 && insn.is_relaxable) > { > diff --git a/gas/config/tc-bpf.h b/gas/config/tc-bpf.h > index 9fb71eddd14..06096ef5926 100644 > --- a/gas/config/tc-bpf.h > +++ b/gas/config/tc-bpf.h > @@ -51,6 +51,10 @@ > a jump to offset 0 means jump to the next instruction. */ > #define md_single_noop_insn "ja 0" > > +#define md_parse_name(name, exp, mode, c) \ > + bpf_parse_name (name, exp, mode) > +bool bpf_parse_name (const char *, struct expressionS *, enum expr_mode); > + > #define TC_EQUAL_IN_INSN(c, s) bpf_tc_equal_in_insn ((c), (s)) > extern bool bpf_tc_equal_in_insn (int, char *); > > diff --git a/gas/testsuite/gas/bpf/asm-extra-sym-1.d b/gas/testsuite/gas/bpf/asm-extra-sym-1.d > new file mode 100644 > index 00000000000..113750dd3fd > --- /dev/null > +++ b/gas/testsuite/gas/bpf/asm-extra-sym-1.d > @@ -0,0 +1,7 @@ > +#as: -EL -mdialect=pseudoc > +#nm: --numeric-sort > +#source: asm-extra-sym-1.s > +#name: BPF pseudoc no extra symbols 1 > + > +# Note: there should be no output from nm. > +# Previously a bug in the BPF parser created an UND '*' symbol. > diff --git a/gas/testsuite/gas/bpf/asm-extra-sym-1.s b/gas/testsuite/gas/bpf/asm-extra-sym-1.s > new file mode 100644 > index 00000000000..2cfa605a259 > --- /dev/null > +++ b/gas/testsuite/gas/bpf/asm-extra-sym-1.s > @@ -0,0 +1 @@ > + r2 = *(u32*)(r1 + 8) > diff --git a/gas/testsuite/gas/bpf/bpf.exp b/gas/testsuite/gas/bpf/bpf.exp > index 80f5a1dbc2d..fcbeccd8ecd 100644 > --- a/gas/testsuite/gas/bpf/bpf.exp > +++ b/gas/testsuite/gas/bpf/bpf.exp > @@ -72,4 +72,7 @@ if {[istarget bpf*-*-*]} { > run_dump_test disp16-overflow-relax > run_dump_test disp32-overflow > run_dump_test imm32-overflow > + > + # Test that parser does not create undefined symbols > + run_dump_test asm-extra-sym-1 > }