Skip to contentSkip to conversation

perf build: Fix compiler errors with old capstone

PATCH Superseded Namhyung Kim 3 participants 7 messages Jul 1 – Jul 3 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 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_MIPS64

Also 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.

Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")
---
 tools/perf/util/capstone.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/perf/util/capstone.c +7 -0
index 5ad537fea4360e31..dd7cd97d0a38056e 100644
@@ -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
#1 lore raw
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…

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
index 5ad537fea4360..dd7cd97d0a380 100644
@@ -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
author maintainer
#2 lore raw
Quoted 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] 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
index 5ad537fea4360..dd7cd97d0a380 100644
@@ -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).

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
reviewer
#3 lore raw
In reply to Namhyung Kim
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_MIPS64
Quoted 7 lines

Also 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.

Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")

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
index 5ad537fea4360e31..dd7cd97d0a38056e 100644
@@ -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

author maintainer
#4 lore raw

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_MIPS64
Quoted 7 lines

Also 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.

Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")
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

reviewer
#5 lore raw
Quoted 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_MIPS64
Quoted 7 lines

Also 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.

Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")
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

author maintainer
#6 lore raw
Quoted 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_MIPS64
Quoted 7 lines

Also 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.

Fixes 12c4737f55f2 ("perf capstone: Determine architecture from e_machine")
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