Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions linux_tools/bpf_exp.y
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
enum jmp_type { JTL, JFL, JKL };

extern FILE *yyin;
extern int yylineno;
extern int yylex(void);
extern void yyerror(const char *str);

Expand Down Expand Up @@ -292,13 +293,13 @@ ldx
| OP_LDXB number '*' '(' '[' number ']' '&' number ')' {
if ($2 != 4 || $9 != 0xf) {
fprintf(stderr, "ldxb offset not supported!\n");
exit(0);
exit(1);
} else {
bpf_set_curr_instr(BPF_LDX | BPF_MSH | BPF_B, 0, 0, $6); } }
| OP_LDX number '*' '(' '[' number ']' '&' number ')' {
if ($2 != 4 || $9 != 0xf) {
fprintf(stderr, "ldxb offset not supported!\n");
exit(0);
exit(1);
} else {
bpf_set_curr_instr(BPF_LDX | BPF_MSH | BPF_B, 0, 0, $6); } }
;
Expand Down Expand Up @@ -579,7 +580,7 @@ static void bpf_assert_max(void)
{
if (curr_instr >= BPF_MAXINSNS) {
fprintf(stderr, "only max %u insns allowed!\n", BPF_MAXINSNS);
exit(0);
exit(1);
}
}

Expand Down Expand Up @@ -629,7 +630,7 @@ static int bpf_find_insns_offset(const char *label)

if (ret == -ENOENT) {
fprintf(stderr, "no such label \'%s\'!\n", label);
exit(0);
exit(1);
}

return ret;
Expand All @@ -652,14 +653,26 @@ static void bpf_reduce_k_jumps(void)
}
}

static uint8_t bpf_encode_jt_jf_offset(int off, int i)
{
int delta = off - i - 1;

if (delta < 0 || delta > 255) {
fprintf(stderr, "error: insn #%d jumps to insn #%d, "
"which is out of range\n", i, off);
exit(1);
}
return (uint8_t) delta;
}

static void bpf_reduce_jt_jumps(void)
{
int i;

for (i = 0; i < curr_instr; i++) {
if (labels_jt[i]) {
int off = bpf_find_insns_offset(labels_jt[i]);
out[i].jt = (uint8_t) (off - i -1);
out[i].jt = bpf_encode_jt_jf_offset(off, i);
}
}
}
Expand All @@ -671,7 +684,7 @@ static void bpf_reduce_jf_jumps(void)
for (i = 0; i < curr_instr; i++) {
if (labels_jf[i]) {
int off = bpf_find_insns_offset(labels_jf[i]);
out[i].jf = (uint8_t) (off - i - 1);
out[i].jf = bpf_encode_jt_jf_offset(off, i);
}
}
}
Expand Down Expand Up @@ -758,5 +771,6 @@ void bpf_asm_compile(FILE *fp, bool cstyle)

void yyerror(const char *str)
{
fprintf(stderr, "error: %s at line %d\n", str, yylineno);
exit(1);
}