Fix unexpected token at start of statement error with clang

My life is a litle bit weired. I never got success on first attempt, there is always some errors, bugs. So, now-a-days seeing an error is a normal thing for me. To be honest i think if there were no struggle to get things working, maybe i couldn’t be here. Now come to the actual issue, what error do we have here ? Following terminal snippet has the error that i got while compiling kernel for Realme 2 Pro (stanlee). It was saying error: unexpected token at start of statement.

terminal snippet

[email protected]:~/N00bKernel$ ccache make ARCH=arm64 CC=clang CLANG_TRIPLE=aarch64-linux-gnu- CROSS_COMPILE_PREFIX=aarch64-linux-gnu-
@@@@@@@@@@@ 111 OPPO_BUILD_CUSTOMIZE is 
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CC      kernel/bounds.s
kernel/bounds.c:18:2: error: unexpected token at start of statement
        DEFINE(NR_PAGEFLAGS, __NR_PAGEFLAGS);
        ^
include/linux/kbuild.h:5:25: note: expanded from macro 'DEFINE'
        asm volatile("\n->" #sym " %0 " #val : : "i" (val))
                        ^
<inline asm>:2:1: note: instantiated into assembly here
->NR_PAGEFLAGS $21 __NR_PAGEFLAGS
^
kernel/bounds.c:19:2: error: unexpected token at start of statement
        DEFINE(MAX_NR_ZONES, __MAX_NR_ZONES);
        ^
include/linux/kbuild.h:5:25: note: expanded from macro 'DEFINE'
        asm volatile("\n->" #sym " %0 " #val : : "i" (val))
                        ^
<inline asm>:2:1: note: instantiated into assembly here
->MAX_NR_ZONES $3 __MAX_NR_ZONES
^
kernel/bounds.c:21:2: error: unexpected token at start of statement
        DEFINE(NR_CPUS_BITS, ilog2(CONFIG_NR_CPUS));
        ^
include/linux/kbuild.h:5:25: note: expanded from macro 'DEFINE'
        asm volatile("\n->" #sym " %0 " #val : : "i" (val))
                        ^
<inline asm>:2:1: note: instantiated into assembly here
->NR_CPUS_BITS $3 ilog2(CONFIG_NR_CPUS)
^
kernel/bounds.c:23:2: error: unexpected token at start of statement
        DEFINE(SPINLOCK_SIZE, sizeof(spinlock_t));
        ^
include/linux/kbuild.h:5:25: note: expanded from macro 'DEFINE'
        asm volatile("\n->" #sym " %0 " #val : : "i" (val))
                        ^
<inline asm>:2:1: note: instantiated into assembly here
->SPINLOCK_SIZE $4 sizeof(spinlock_t)
^
4 errors generated.
Kbuild:45: recipe for target 'kernel/bounds.s' failed
make[1]: *** [kernel/bounds.s] Error 1
Makefile:1129: recipe for target 'prepare0' failed
make: *** [prepare0] Error 2

Problem analysis

KBuild abuses the asm statement to write to a file and clang chokes about these invalid asm statements. Hack it even more by fooling this is actual valid asm code. This is fixed in commit af41c50628c23bcfbe1f56756469c2e4085bc1d0.

Download patch

Below patch(es) can be applied to fix this issue:

o EOP o