https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128807

            Bug ID: 128807
           Summary: BPF: -mcpu is not passed to gas as -misa-spec, v4
                    instructions accepted with -mcpu=v3
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mumumu0722 at gmail dot com
  Target Milestone: ---

With -mcpu=v3, GCC accepts a v4 instruction in inline asm. The BPF driver does
not pass -misa-spec to gas, so gas uses its default (v5 in trunk, v4 in 2.44).

Example:
unsigned long long prog(void)
{
    unsigned long long r;
    /* sign-extending move: an ISA v4 instruction */
    __asm__ volatile("r1 = 200\n\t%0 = (s8)r1" : "=r"(r) : : "r1");
    return r;
}

GCC runs gas without -misa-spec, so the instruction is accepted:
$ bpf-unknown-none-gcc --version | head -1
bpf-unknown-none-gcc (GCC) 17.0.0 20261115 (experimental)
$ bpf-unknown-none-gcc -mcpu=v3 -c test.c
$ bpf-unknown-none-gcc -mcpu=v3 -Wa,-misa-spec=v3 -c test.c
/tmp/cc5eoHLE.s: Assembler messages:
test.c:6: Error: `r0 =(s8)r1' requires ISA spec v4 or later

The object built with -mcpu=v3 is also marked as ISA v5 in the ELF header:

$ bpf-unknown-none-gcc -mcpu=v3 -c test.c
$ bpf-unknown-none-readelf -h test.o | grep Flags
  Flags:                             0x5, CPU Version: 5

Expected: with -mcpu=v3, GCC passes -misa-spec=v3 to gas, so the instruction is
rejected as with -Wa,-misa-spec=v3 above. Objects targeting v3 should be marked
CPU Version 3.

Compiler Explorer (GCC trunk): https://godbolt.org/z/fPYq6TY8q

Found by automated testing.

Reply via email to