perf build: Fix compiler errors with old capstone
Namhyung Kim
maintainerPATCH 1/1 perf build: Fix compiler errors with old capstone
It seems RISCV was added in capstone version 5. Unfortunately they are enum constants so cannot check with #ifdef but anyway we can define the symbols. Let's do it using the version number to avoid build errors. It'll fail at runtime though.
util/capstone.c: In function 'e_machine_to_capstone':
util/capstone.c:186:25: error: 'CS_ARCH_RISCV' undeclared (first use in this function);
did you mean 'CS_ARCH_SYSZ'?
186 | *arch = CS_ARCH_RISCV;
| ^~~~~~~~~~~~~
| CS_ARCH_SYSZ util/capstone.c:186:25: note: each undeclared identifier is reported only once for each function it appears in
util/capstone.c:187:34: error: 'CS_MODE_RISCV64' undeclared (first use in this function);
did you mean 'CS_MODE_MIPS64'?
187 | *mode |= (is64 ? CS_MODE_RISCV64 : CS_MODE_RISCV32) | CS_MODE_RISCVC;
| ^~~~~~~~~~~~~~~
| CS_MODE_MIPS64Also note that capstone renamed CS_MODE_RISCVC to CS_MODE_RISCV_C which would cause a different build failure on latest versions. It's reported in https://github.com/capstone-engine/capstone/issues/2977 so I think they will add compatibility layer to prevent the error.
--- tools/perf/util/capstone.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/perf/util/capstone.c +7 -0
@@ -24,6 +24,13 @@#include "symbol.h" #include "thread.h" +#if CS_VERSION_MAJOR <5 +#define CS_ARCH_RISCV CS_ARCH_MAX +#define CS_MODE_RISCV32 1 +#define CS_MODE_RISCV64 2 +#define CS_MODE_RISCVC 4 +#endif +#ifdef LIBCAPSTONE_DLOPEN static void *perf_cs_dll_handle (void ) {
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Defining CS_ARCH_RISCV as CS_ARCH_MAX causes incorrect architecture selection when dynamically…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Defining CS_ARCH_RISCV as CS_ARCH_MAX causes incorrect architecture selection when dynamically loading newer Capstone versions. --
commit c55db50010b415e5aa95bfca7819cc985aa51d2f
Author: Namhyung Kim <[email protected]>
perf build: Fix compiler errors with old capstone
This commit fixes a build error on systems with Capstone versions older than 5 by defining missing RISC-V architecture and mode constants. It uses the major version macro to conditionally define these symbols.
tools/perf/util/capstone.c line 28
Quoted 10 lines
tools/perf/util/capstone.c +2
-0@@ -24,6 +24,13 @@#include "symbol.h" #include "thread.h" +#if CS_VERSION_MAJOR <5 +#define CS_ARCH_RISCV CS_ARCH_MAX
[Severity: High]
Does mapping CS_ARCH_RISCV to CS_ARCH_MAX cause ABI compatibility issues?
If perf is compiled against Capstone headers < 5, it will use the integer value of CS_ARCH_MAX for CS_ARCH_RISCV. If the resulting binary is later run on a system where libcapstone.so has been upgraded to version 5+, passing this hardcoded value to cs_open() might silently initialize a completely different architecture that was appended to the enum in V5 (such as WASM or BPF).
Could this result in cs_open() succeeding but silently producing garbage disassembly for RISC-V targets?
Quoted 4 lines
tools/perf/util/capstone.c +4
-0+#define CS_MODE_RISCV32 1 +#define CS_MODE_RISCV64 2 +#define CS_MODE_RISCVC 4 +#endif
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerQuoted 34 lines · On Wed, Jul 01, 2026 at 06:38:29PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] DefiningCS_ARCH_RISCVasCS_ARCH_MAXcauses incorrect architecture selection when dynamically loading newer Capstone versions. --commit
c55db50010b415e5aa95bfca7819cc985aa51d2f
Author: Namhyung Kim <[email protected]>perf build: Fix compiler errors with old capstone
This commit fixes a build error on systems with Capstone versions older than 5 by defining missing RISC-V architecture and mode constants. It uses the major version macro to conditionally define these symbols.
tools/perf/util/capstone.c line 28
Quoted 10 lines
tools/perf/util/capstone.c +2
-0@@ -24,6 +24,13 @@#include "symbol.h" #include "thread.h" +#if CS_VERSION_MAJOR <5 +#define CS_ARCH_RISCV CS_ARCH_MAX[Severity: High]
Does mappingCS_ARCH_RISCVtoCS_ARCH_MAXcause ABI compatibility issues?If perf is compiled against Capstone headers < 5, it will use the integer value of
CS_ARCH_MAXforCS_ARCH_RISCV. If the resulting binary is later run on a system where libcapstone.so has been upgraded to version 5+, passing this hardcoded value tocs_open()might silently initialize a completely different architecture that was appended to the enum in V5 (such as WASM or BPF).
Quoted 2 lines
Could this result in
cs_open()succeeding but silently producing garbage disassembly for RISC-V targets?
Ok, I'll use the actual number from v5 then.
Thanks,
Namhyung
Quoted 10 lines
Quoted 5 lines
tools/perf/util/capstone.c +4
-0+#define CS_MODE_RISCV32 1 +#define CS_MODE_RISCV64 2 +#define CS_MODE_RISCVC 4 +#endif [ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerQuoted 20 lines · Namhyung Kim
It seems RISCV was added in capstone version 5. Unfortunately they are enum constants so cannot check with #ifdef but anyway we can define the symbols. Let's do it using the version number to avoid build errors. It'll fail at runtime though.
util/capstone.c: In function 'e_machine_to_capstone':
util/capstone.c:186:25: error: 'CS_ARCH_RISCV' undeclared (first use in this function);did you mean 'CS_ARCH_SYSZ'? 186 | *arch = CS_ARCH_RISCV; | ^~~~~~~~~~~~~ | CS_ARCH_SYSZ
util/capstone.c:186:25: note: each undeclared identifier is reported only once for each function it appears in
util/capstone.c:187:34: error: 'CS_MODE_RISCV64' undeclared (first use in this function);did you mean 'CS_MODE_MIPS64'? 187 | *mode |= (is64 ? CS_MODE_RISCV64 : CS_MODE_RISCV32) | CS_MODE_RISCVC; | ^~~~~~~~~~~~~~~ | CS_MODE_MIPS64
Quoted 7 lines
Also note that capstone renamed
CS_MODE_RISCVCtoCS_MODE_RISCV_Cwhich would cause a different build failure on latest versions. It's reported in https://github.com/capstone-engine/capstone/issues/2977 so I think they will add compatibility layer to prevent the error.Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")Signed-off-by Namhyung Kim <[email protected]>
Perhaps update the feature test from:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/build/feature/test-libcapstone.c?h=perf-tools-next
``` cs_open(CS_ARCH_X86, CS_MODE_64, &handle); ```
to be CS_ARCH_RISCV? That way we won't detect old libcapstones and avoid cluttering up the code.
Thanks,
Ian
Quoted 25 lines
--- tools/perf/util/capstone.c | 7 +++++++ 1 file changed, 7 insertions(+)tools/perf/util/capstone.c +7
-1@@ -24,6 +24,13 @@#include "symbol.h" #include "thread.h" +#if CS_VERSION_MAJOR <5 +#define CS_ARCH_RISCV CS_ARCH_MAX +#define CS_MODE_RISCV32 1 +#define CS_MODE_RISCV64 2 +#define CS_MODE_RISCVC 4 +#endif +#ifdef LIBCAPSTONE_DLOPEN static void *perf_cs_dll_handle (void ) { --2.55.0.rc0.799.gd6f94ed593-goog
Namhyung Kim
maintainerHi Ian,
Quoted 29 lines · Ian Rogers
Quoted 20 lines · Namhyung Kim
It seems RISCV was added in capstone version 5. Unfortunately they are enum constants so cannot check with #ifdef but anyway we can define the symbols. Let's do it using the version number to avoid build errors. It'll fail at runtime though.
util/capstone.c: In function 'e_machine_to_capstone':
util/capstone.c:186:25: error: 'CS_ARCH_RISCV' undeclared (first use in this function);did you mean 'CS_ARCH_SYSZ'? 186 | *arch = CS_ARCH_RISCV; | ^~~~~~~~~~~~~ | CS_ARCH_SYSZ
util/capstone.c:186:25: note: each undeclared identifier is reported only once for each function it appears in
util/capstone.c:187:34: error: 'CS_MODE_RISCV64' undeclared (first use in this function);did you mean 'CS_MODE_MIPS64'? 187 | *mode |= (is64 ? CS_MODE_RISCV64 : CS_MODE_RISCV32) | CS_MODE_RISCVC; | ^~~~~~~~~~~~~~~ | CS_MODE_MIPS64Quoted 7 lines
Also note that capstone renamed
CS_MODE_RISCVCtoCS_MODE_RISCV_Cwhich would cause a different build failure on latest versions. It's reported in https://github.com/capstone-engine/capstone/issues/2977 so I think they will add compatibility layer to prevent the error.Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")Signed-off-by Namhyung Kim <[email protected]>
Quoted 7 lines
Perhaps update the feature test from:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/build/feature/test-libcapstone.c?h=perf-tools-next``` cs_open(CS_ARCH_X86, CS_MODE_64, &handle); ```to be
CS_ARCH_RISCV? That way we won't detect old libcapstones and avoid cluttering up the code.
Then it'll drop support for capstone version 4 which works fine on some architectures. Maybe we can add another feature check for capstone RISC-V support but I think it's better to define the RISCV symbols here.
What do you think?
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 41 lines · Namhyung Kim
Hi Ian,
Quoted 29 lines · Ian Rogers
Quoted 20 lines · Namhyung Kim
It seems RISCV was added in capstone version 5. Unfortunately they are enum constants so cannot check with #ifdef but anyway we can define the symbols. Let's do it using the version number to avoid build errors. It'll fail at runtime though.
util/capstone.c: In function 'e_machine_to_capstone':
util/capstone.c:186:25: error: 'CS_ARCH_RISCV' undeclared (first use in this function);did you mean 'CS_ARCH_SYSZ'? 186 | *arch = CS_ARCH_RISCV; | ^~~~~~~~~~~~~ | CS_ARCH_SYSZ
util/capstone.c:186:25: note: each undeclared identifier is reported only once for each function it appears in
util/capstone.c:187:34: error: 'CS_MODE_RISCV64' undeclared (first use in this function);did you mean 'CS_MODE_MIPS64'? 187 | *mode |= (is64 ? CS_MODE_RISCV64 : CS_MODE_RISCV32) | CS_MODE_RISCVC; | ^~~~~~~~~~~~~~~ | CS_MODE_MIPS64Quoted 7 lines
Also note that capstone renamed
CS_MODE_RISCVCtoCS_MODE_RISCV_Cwhich would cause a different build failure on latest versions. It's reported in https://github.com/capstone-engine/capstone/issues/2977 so I think they will add compatibility layer to prevent the error.Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")Signed-off-by Namhyung Kim <[email protected]>Quoted 7 lines
Perhaps update the feature test from:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/build/feature/test-libcapstone.c?h=perf-tools-next``` cs_open(CS_ARCH_X86, CS_MODE_64, &handle); ```to be
CS_ARCH_RISCV? That way we won't detect old libcapstones and avoid cluttering up the code.
Quoted 5 lines
Then it'll drop support for capstone version 4 which works fine on some architectures. Maybe we can add another feature check for capstone RISC-V support but I think it's better to define the RISCV symbols here.
What do you think?
Arnaldo and I also discussed this in a thread, but I can't find it right now. I believe the RISC-V support in libcapstone is now 8 years old:
https://github.com/capstone-engine/capstone/issues/966
so it seemed unfortunate to add shims to support older libcapstone versions.
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 46 lines · Ian Rogers
Quoted 45 lines · Namhyung Kim
Hi Ian,
Quoted 29 lines · Ian Rogers
Quoted 20 lines · Namhyung Kim
It seems RISCV was added in capstone version 5. Unfortunately they are enum constants so cannot check with #ifdef but anyway we can define the symbols. Let's do it using the version number to avoid build errors. It'll fail at runtime though.
util/capstone.c: In function 'e_machine_to_capstone':
util/capstone.c:186:25: error: 'CS_ARCH_RISCV' undeclared (first use in this function);did you mean 'CS_ARCH_SYSZ'? 186 | *arch = CS_ARCH_RISCV; | ^~~~~~~~~~~~~ | CS_ARCH_SYSZ
util/capstone.c:186:25: note: each undeclared identifier is reported only once for each function it appears in
util/capstone.c:187:34: error: 'CS_MODE_RISCV64' undeclared (first use in this function);did you mean 'CS_MODE_MIPS64'? 187 | *mode |= (is64 ? CS_MODE_RISCV64 : CS_MODE_RISCV32) | CS_MODE_RISCVC; | ^~~~~~~~~~~~~~~ | CS_MODE_MIPS64Quoted 7 lines
Also note that capstone renamed
CS_MODE_RISCVCtoCS_MODE_RISCV_Cwhich would cause a different build failure on latest versions. It's reported in https://github.com/capstone-engine/capstone/issues/2977 so I think they will add compatibility layer to prevent the error.Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")Signed-off-by Namhyung Kim <[email protected]>Quoted 7 lines
Perhaps update the feature test from:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/build/feature/test-libcapstone.c?h=perf-tools-next``` cs_open(CS_ARCH_X86, CS_MODE_64, &handle); ```to be
CS_ARCH_RISCV? That way we won't detect old libcapstones and avoid cluttering up the code.Then it'll drop support for capstone version 4 which works fine on some architectures. Maybe we can add another feature check for capstone RISC-V support but I think it's better to define the RISCV symbols here.
Quoted 7 lines
Quoted 1 line
What do you think?
Arnaldo and I also discussed this in a thread, but I can't find it right now. I believe the RISC-V support in libcapstone is now 8 years old:
https://github.com/capstone-engine/capstone/issues/966
so it seemed unfortunate to add shims to support older libcapstone versions.
It seems v5.0 was released 3 years ago (2023-07-05).
https://github.com/capstone-engine/capstone/releases/tag/5.0
Actually it's added to v5.0-rc1 which was released a year before (2022-02-27) but I guess many distro didn't ship the -rc.
https://github.com/capstone-engine/capstone/releases/tag/5.0-rc1
Thanks,
Namhyung