aboutsummaryrefslogtreecommitdiff
path: root/gas/config/tc-bpf.c
diff options
context:
space:
mode:
authorJose E. Marchesi <jose.marchesi@oracle.com>2023-11-18 18:12:44 +0100
committerJose E. Marchesi <jose.marchesi@oracle.com>2023-11-18 18:17:26 +0100
commit8fbb497b7202f404d8914252d44d2ee190a0e172 (patch)
treef66854caee10fdcb22186c67eedae3aebe24f1f5 /gas/config/tc-bpf.c
parent26c7a0ea380bedb86ff3b5a9ed252d8d2557a0c5 (diff)
downloadgdb-8fbb497b7202f404d8914252d44d2ee190a0e172.zip
gdb-8fbb497b7202f404d8914252d44d2ee190a0e172.tar.gz
gdb-8fbb497b7202f404d8914252d44d2ee190a0e172.tar.bz2
gas: bpf: do not allow referring to register names as symbols in operands
2023-11-18 Jose E. Marchesi <jemarch@gnu.org> * config/tc-bpf.c (parse_bpf_register): Move before bpf_parse_name. (bpf_parse_name): Do not allow using symbols that are also register names as operands in pseudo-c syntax. * testsuite/gas/bpf/regs-for-symbols-pseudoc.d: New file. * testsuite/gas/bpf/regs-for-symbols-pseudoc.s: Likewise. * testsuite/gas/bpf/regs-for-symbols-pseudoc.l: Likewise. * doc/c-bpf.texi (BPF Registers): Document that it is not possible to refer to register names as symbols in instruction operands.
Diffstat (limited to 'gas/config/tc-bpf.c')
-rw-r--r--gas/config/tc-bpf.c113
1 files changed, 65 insertions, 48 deletions
diff --git a/gas/config/tc-bpf.c b/gas/config/tc-bpf.c
index 3122f80..45abc3c 100644
--- a/gas/config/tc-bpf.c
+++ b/gas/config/tc-bpf.c
@@ -1255,6 +1255,54 @@ parse_expression (char *s, expressionS *exp)
return s;
}
+/* 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. */
+
+static char *
+parse_bpf_register (char *s, char rw, uint8_t *regno)
+{
+ if (asm_dialect == DIALECT_NORMAL)
+ {
+ rw = 'r';
+ if (*s != '%')
+ return NULL;
+ s += 1;
+
+ if (*s == 'f' && *(s + 1) == 'p')
+ {
+ *regno = 10;
+ s += 2;
+ return s;
+ }
+ }
+
+ if (*s != rw)
+ return NULL;
+ s += 1;
+
+ if (*s == '1')
+ {
+ if (*(s + 1) == '0')
+ {
+ *regno = 10;
+ s += 2;
+ }
+ else
+ {
+ *regno = 1;
+ s += 1;
+ }
+ }
+ else if (*s >= '0' && *s <= '9')
+ {
+ *regno = *s - '0';
+ s += 1;
+ }
+
+ return s;
+}
+
/* Symbols created by this parse, but not yet committed to the real
symbol table. */
static symbolS *deferred_sym_rootP;
@@ -1283,6 +1331,23 @@ bpf_parse_name (const char *name, expressionS *exp, enum expr_mode mode)
gas_assert (mode == expr_normal);
+ /* Pseudo-C syntax uses unprefixed register names like r2 or w3.
+ Since many instructions take either a register or an
+ immediate/expression, we should not allow references to symbols
+ with these names in operands. */
+ if (asm_dialect == DIALECT_PSEUDOC)
+ {
+ uint8_t regno;
+
+ if (parse_bpf_register ((char *) name, 'r', &regno)
+ || parse_bpf_register ((char *) name, 'w', &regno))
+ {
+ as_bad (_("unexpected register name `%s' in expression"),
+ name);
+ return false;
+ }
+ }
+
if (symbol_find (name) != NULL)
return false;
@@ -1320,54 +1385,6 @@ bpf_parse_name (const char *name, expressionS *exp, enum expr_mode mode)
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. */
-
-static char *
-parse_bpf_register (char *s, char rw, uint8_t *regno)
-{
- if (asm_dialect == DIALECT_NORMAL)
- {
- rw = 'r';
- if (*s != '%')
- return NULL;
- s += 1;
-
- if (*s == 'f' && *(s + 1) == 'p')
- {
- *regno = 10;
- s += 2;
- return s;
- }
- }
-
- if (*s != rw)
- return NULL;
- s += 1;
-
- if (*s == '1')
- {
- if (*(s + 1) == '0')
- {
- *regno = 10;
- s += 2;
- }
- else
- {
- *regno = 1;
- s += 1;
- }
- }
- else if (*s >= '0' && *s <= '9')
- {
- *regno = *s - '0';
- s += 1;
- }
-
- return s;
-}
-
/* Collect a parse error message. */
static int partial_match_length = 0;