[2/2] bpf: remove symbols created during failed parse
Checks
Commit Message
Parsing the BPF pseudo-c asm syntax requires attempting to parse an
instruction using a template that may later be determined to not match.
During this parsing, a call to expression () may end up creating one or
more symbols. If the parsed instruction is later determined to not
match the template, then any symbols created during this process should
be discarded.
If such unused symbols are not discarded, they impede the loading of the
resulting BPF object by the Linux kernel.
gas/
* config/tc-bpf.c (last_parsed_expr, old_symbol_lastP): New.
(parse_expression): Track last_parsed_expr and old_symbol_lastP.
(parse_error): Cleanup symbols created during a failed parse.
* testsuite/gas/bpf/asm-extra-sym-1.d: New.
* testsuite/gas/bpf/asm-extra-sym-1.s: New.
* testsuite/gas/bpf/asm-extra-sym-2.d: New.
* testsuite/gas/bpf/asm-extra-sym-2.s: New.
* testsuite/gas/bpf/bpf.exp: Run new tests.
---
gas/config/tc-bpf.c | 30 +++++++++++++++++++++++++
gas/testsuite/gas/bpf/asm-extra-sym-1.d | 7 ++++++
gas/testsuite/gas/bpf/asm-extra-sym-1.s | 1 +
gas/testsuite/gas/bpf/asm-extra-sym-2.d | 7 ++++++
gas/testsuite/gas/bpf/asm-extra-sym-2.s | 8 +++++++
gas/testsuite/gas/bpf/bpf.exp | 4 ++++
6 files changed, 57 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
create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-2.d
create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-2.s
@@ -1223,6 +1223,8 @@ 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 expressionS *last_parsed_expr = NULL;
+static symbolS *old_symbol_lastP = NULL;
static char *
parse_expression (char *s, expressionS *exp)
@@ -1232,10 +1234,13 @@ parse_expression (char *s, expressionS *exp)
exp_parse_failed = 0;
input_line_pointer = s;
+ old_symbol_lastP = symbol_lastP;
expression (exp);
s = input_line_pointer;
input_line_pointer = saved_input_line_pointer;
+ last_parsed_expr = exp;
+
switch (exp->X_op == O_absent || exp_parse_failed)
return NULL;
@@ -1317,6 +1322,25 @@ parse_error (int length, const char *fmt, ...)
va_end (args);
partial_match_length = length;
}
+
+ /* Cleanup any symbols created during the failed parsing. */
+ if (last_parsed_expr
+ && (last_parsed_expr->X_add_symbol || last_parsed_expr->X_op_symbol))
+ {
+ /* NOTE: this logic exploits the implementation detail that a symbol
+ created by expression () during parsing is appended to the list
+ rather than potentially being inserted somewhere in the middle. */
+ symbolS *sym = symbol_lastP;
+ while (sym != old_symbol_lastP)
+ {
+ /* Must have created at least one symbol. */
+ symbol_remove (sym, &symbol_rootP, &symbol_lastP);
+ symbol_table_remove (sym);
+ sym = symbol_lastP;
+ }
+
+ old_symbol_lastP = symbol_lastP;
+ }
}
/* Assemble a machine instruction in STR and emit the frags/bytes it
@@ -1368,6 +1392,12 @@ md_assemble (char *str ATTRIBUTE_UNUSED)
if (opcode->version > isa_spec)
continue;
+ /* Track expression parsed while trying this opcode. If this turns
+ out to be the wrong opcode, we need to undo side effects of the
+ expression parsing, such as creating a new undefined symbol.
+ Set by parse_expression () and used by parse_error (). */
+ last_parsed_expr = NULL;
+
memset (&insn, 0, sizeof (struct bpf_insn));
insn.size = 8;
for (s = str, p = template; *p != '\0';)
new file mode 100644
@@ -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 created an UND '*' symbol.
new file mode 100644
@@ -0,0 +1 @@
+ r2 = *(u32*)(r1 + 8)
new file mode 100644
@@ -0,0 +1,7 @@
+#as: -EL -mdialect=pseudoc
+#nm: --numeric-sort
+#source: asm-extra-sym-2.s
+#name: BPF pseudoc no extra symbols 2
+
+[0-9a-f]+ t main
+[0-9a-f]+ t foo
new file mode 100644
@@ -0,0 +1,8 @@
+
+ .text
+main:
+ call foo
+ call foo
+foo:
+ r1 = 1
+ exit
@@ -72,4 +72,8 @@ 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
+ run_dump_test asm-extra-sym-2
}