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 EDCC33858C39 for ; Fri, 17 Nov 2023 18:54:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EDCC33858C39 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 EDCC33858C39 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1700247289; cv=pass; b=jrIKV79kqy0B9IkFUipTeA2L5Sr39a8BWuK4DAVhQ5HFWvXMuqJujf6+RjkeeZpulJ+/gYESG59z2l0XX+Po8CMSqPUtks/v/2m1k2NcKQTXBr6tf+y5XbqyPtviZx408p/FRMGIQpXve90XO/xFqnmYYToAxQnGCJ/PkpEUNkA= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1700247289; c=relaxed/simple; bh=FMaFKoQPeqeIzEjr/qXD4o47Y/1eTWZgQnxtmbQzPeQ=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=tB9aNUA5jjBSplE7B+WR4716GleKmJ05Tn8PlUKskELyl1HhrUmx+JCRXdNk7v20TT9f3gDjD+NX46K05TVilMwJM4NItBSV62wjUrZ2JYv3IKpafghF4zDDnsd0gSk73Sjk7YZf9+iqZmKWVptq/aO/UxjANpKdgh+U3/v8bR8= ARC-Authentication-Results: i=2; server2.sourceware.org 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 3AHIO2JG004271; Fri, 17 Nov 2023 18:54:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : content-type : mime-version; s=corp-2023-03-30; bh=DPr5hoXNhUfpllR4zyV3U/+TltlTDMgnqXVcibvG5Sg=; b=CQ6WFvO05S1oi5hyE6dD6u5obtAd+nO+KWOJpQHUSg6fwQvo3exuhBgn5Pp/Fbqtswse Em3miHSWCa/gyeOwygT92ETR2QHJiiqw181MQB0KhAHVawzkFs40YgP4TBcn56xI8KLP yH4+qSLImHfem05P7JaGYzixPFYITD7xQ1bI2r8edKX3DgPQlyFXZFBuoMl7dQLAV0v4 laFr4oM81G+LIW8k87r91VZ1M8SBiP0fDKZ9uJnNQCrktjUKeNfSz11MtTrkDcUm2lor O5417T5PjrVatz6rTVzqMc7ItgkZ58iFE9ZVLyKk+ZFYXWKHpRSM1LI0m+IpB51j77Io 1A== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ua2na62pg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 17 Nov 2023 18:54:45 +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 3AHIjKKB015693; Fri, 17 Nov 2023 18:54:44 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2101.outbound.protection.outlook.com [104.47.70.101]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3uaxq2qb80-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 17 Nov 2023 18:54:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MT2xhRRZPYOR6eTVXlyUavqN0T/36s8db4lz5Iw39bG9K/uGK60suNENoZ4OtpDn6aWe6RBxHnBO20q400AT5+RUVPfSTqLO7H92a5Yy1p0baAfYmvrDy1N3gYKW4EkKjW+zrGbA2iwWnzGGfbKAlxKtQG3EJDwxqJuqnFPluHb8pyYl4QpBt2q9fcKZvfcAVim7hHRUxhg2N5Eb1rjHsN2h+KRXRmpP9/HExL4tyFndkVqQF94LxRbHrA4FtZDA0OfQe2bdXyehdJSlKULb1EmzHS8iKALP4mceV6uQMT7Vcfy5yvFD8nry+SzTpNPuz5WQmhy+aOpev1KloXp5uQ== 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=DPr5hoXNhUfpllR4zyV3U/+TltlTDMgnqXVcibvG5Sg=; b=j8/LB8umVR21K3Lesz4iz0QrjYPwcyNbzDYl3sy85tPNmLXIPRGIcpjEZ6ty1pyPbFqACa+KbbOaQIgrLFL0mBia7bjqFvF7quZwe7TbdpQ+OeB/DR/5fp32t8s2PcLkqrxeWMxlmMDFuMUF/q8/tXx/gCAyRVmgpOlWa88DT01jRvO5tFpGmGM9fIJtSC/bhq36URfLOjTTewiE27kxPWx45l0edQExomV3DjGbyvhpIE22f+cp8bCzsjpwowGA+NIIU0zIXBOmSeLkRQ7UJvkh1PR/lHqspm3JIATH2w948fqhRpAxVWVnQ07VgT2Hy5OnX77pUtYe0DiDTr5fAw== 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=DPr5hoXNhUfpllR4zyV3U/+TltlTDMgnqXVcibvG5Sg=; b=C6wg/oQI1lJ377urusEhJ9qxb+vQB+NqVRhXe07LLds7MhI/5msHcoNU6xZbToJPP0GQn64htv2h1wzv0ZwFrwoQaVDgsgfY/4bQ4syKjhXeQLfDsqjHvIDzjfyh9cy/LR2wFU/o++Lh7DIqSoQ0dPBGFbGpypR8j3kAhZhIGus= Received: from MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) by PH7PR10MB5769.namprd10.prod.outlook.com (2603:10b6:510:125::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.23; Fri, 17 Nov 2023 18:54:41 +0000 Received: from MN2PR10MB3213.namprd10.prod.outlook.com ([fe80::d8:db85:8025:ed64]) by MN2PR10MB3213.namprd10.prod.outlook.com ([fe80::d8:db85:8025:ed64%7]) with mapi id 15.20.7002.022; Fri, 17 Nov 2023 18:54:41 +0000 From: David Faust To: binutils@sourceware.org Cc: jose.marchesi@oracle.com, jbeulich@suse.com Subject: [PATCH v2] bpf: avoid creating wrong symbols while parsing Date: Fri, 17 Nov 2023 10:54:28 -0800 Message-ID: <20231117185428.10823-1-david.faust@oracle.com> X-Mailer: git-send-email 2.41.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SJ0PR05CA0180.namprd05.prod.outlook.com (2603:10b6:a03:339::35) To MN2PR10MB3213.namprd10.prod.outlook.com (2603:10b6:208:131::33) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB3213:EE_|PH7PR10MB5769:EE_ X-MS-Office365-Filtering-Correlation-Id: 5c552e4e-6fa0-4445-8765-08dbe79ea814 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kWKhwiJW3Rjc3VWO4RNucjNVfhDNG1xZDf6FvGF/AqBYeCjQnpbseXUoYbKwwZKVdBjl927UJvRKNZne/CNVvjs9awac5PdwSz5M+2KcaAdPWdRk5tsDrT7Aqooa6i4W4H8HmSdomsnOmv2l4oP1XDRQbDjimVatT2vQIYoLM/6qspbxUrifezUTztXZ9pcda988itl2Il4waeM4OJbhgsF/AmKAc5hZFvRxHy3Cugle+KqnpuykU6VFRWomMcLUD4ltJoZ4hl8mHpKwnnl1JRky3R4nJkt8IFmko4Elm8dTorekkCjGa6C0nVNvLV9Te69gXfd73/N29WRBa99ZxkUhhodstHi+HjL++GrGtNvtx5sJryYUjFrjkT/uOySAEnBgyfzZ7K+JJs8nVlsGVrkK5lT+BM2GZYqwUil3ulGt6Yoj4Nm8xQ8Ceo4IvhmFMiteDhBsgVMbmNl0jH5WopzDsiwLkqAV2zHLnjFNlz2IvW++1xQekAkFj/yAjSWFW95D/i6gV+CF6UoVTtAqHOwtw5AikhAV3u74Pem8Hw4= 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:(13230031)(366004)(376002)(39860400002)(396003)(136003)(346002)(230922051799003)(451199024)(64100799003)(1800799009)(186009)(44832011)(6486002)(966005)(478600001)(6506007)(66556008)(6666004)(66476007)(66946007)(6916009)(8676002)(8936002)(4326008)(316002)(83380400001)(2906002)(38100700002)(41300700001)(36756003)(1076003)(2616005)(5660300002)(26005)(6512007)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ryGxd5tiR4qKQxFk82uKNiRk8uQzfVQ+KaOuXv/v/408eoRDqCVDKy4mAKlp?= =?us-ascii?Q?ArOJUcv1MT9EIjRAX2cC8WzTdiOKMsjYyO3+D83wVPDi2uwpsQvm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ayvzd9cceu5GO1wtYjdhcVvBdgTcpb85ZCGbNlbMFKha0+Wh2YGwCkidyCk0Bxhew9NLtgzeY30zHx3W2fFHYR+6c/yhAH5620LO4NmpAiS6wlKTHJXoP+y1X80s/wwTVLleySaSUkSUVC8JE12eiPUHxqd4awaJvwukmhiXDcvycLO3k00icIdaL5pnvJRHU4oNM6oni7Z+d0JWhcJ2Qjo4cnTeisKyYv3dKk8FJdMZ0KGx3/wN+j/jCvf2HNG/FnbHejYBHbbcaD6a2mzoUYyAz2K9+EHm0Ue0QhjIjVjjMWRM9PPvM/LR6qUItKYlRkuUby+f3CvnvO2Uu0Iv8loUR3UVS1fXDQfuQpkEa46XPvAm+aN3XQoq1Nn0o/brJe2BnA+iruXJrJu1DE8ADRcbizVvzFfN2gy6e9+tyF7gG4UJaaZpbvX2e6TCYhhCULn8NletwGj1kB1SqwmIkiZ1Sdf1YCrXGUy4hiPDQtuNUaC13HoFseEOLeisuTSQ/Fkkm9KklwlbT0lKXO18PixKhU4Ob+OEZ7558+4a6gqmS4qpF3IzdpYPDWQtIBZ5OOMB8aKSGQU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5c552e4e-6fa0-4445-8765-08dbe79ea814 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB3213.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Nov 2023 18:54:41.5061 (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: OtA9WX/Ggd/xF+A5MxgmAtBTyOMs7wV/vwWeTEmQsG97CSuYVo8xCKi8W53HXjIpc3gxm+Ij4MKmGbEzdH//0w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB5769 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-2311170143 X-Proofpoint-GUID: 8Rm4rfAHzw2yjS0tzp73-UcY86bqP5ih X-Proofpoint-ORIG-GUID: 8Rm4rfAHzw2yjS0tzp73-UcY86bqP5ih X-Spam-Status: No, score=-13.3 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: [ 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; + + 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 } -- 2.42.0