Hello people.
I want to run some binary code from inside my cpp application. I had it running without any issue on my raspberry pi zero 2W.
However, this application needed more power, so I decided to switch to a Rpi 4B. It sounded easy, but I am struggling with it.
This, due to getting random "Illegal Instruction" crashes when running the application.
The idea is to generate a binary file from an assembly file, then load it using mmap into an array, and execute it as a function.
I use mmap to copy the file contents into executable memory.
(Running on Bookworm lite 64bit)
When running this program, output is that sometimes it prints "Result: 42" and sometimes it prints "Illegal instruction"
=========================
I have created a very simple version that still gives "Illegal instruction" results randomly.
The following is the code so anyone can confirm whether it works on their rpi 4B
.as file:
=========================
.cpp file (I am ignoring include files to make it more readable)
=============
Commands:
as -o example.o example.s
objcopy -O binary example.o example.bin
g++ -o my_program main.cpp
I have even tried the following with the same result:
g++ -o my_program main.cpp -mcpu=cortex-a72 -mtune=cortex-a72
Any help will be very much appreciated![Smile :)]()
I want to run some binary code from inside my cpp application. I had it running without any issue on my raspberry pi zero 2W.
However, this application needed more power, so I decided to switch to a Rpi 4B. It sounded easy, but I am struggling with it.
This, due to getting random "Illegal Instruction" crashes when running the application.
The idea is to generate a binary file from an assembly file, then load it using mmap into an array, and execute it as a function.
I use mmap to copy the file contents into executable memory.
(Running on Bookworm lite 64bit)
When running this program, output is that sometimes it prints "Result: 42" and sometimes it prints "Illegal instruction"
=========================
I have created a very simple version that still gives "Illegal instruction" results randomly.
The following is the code so anyone can confirm whether it works on their rpi 4B
.as file:
Code:
.global my_functionmy_function: mov x0, #42 ret.cpp file (I am ignoring include files to make it more readable)
Code:
void* load_binary(const char* filename, size_t& size) { std::ifstream file(filename, std::ios::binary | std::ios::ate); size = file.tellg(); file.seekg(0, std::ios::beg); void* buffer = mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (buffer == MAP_FAILED) { return nullptr; } file.read(reinterpret_cast<char*>(buffer), size); return buffer;}int main() { size_t size; void* code = load_binary("example.bin", size); if (!code) { std::cerr << "Failed to load binary code" << std::endl; return 1; } auto func = reinterpret_cast<int(*)()>(code); int result = func(); std::cout << "Result: " << result << std::endl; munmap(code, size); return 0;}Commands:
as -o example.o example.s
objcopy -O binary example.o example.bin
g++ -o my_program main.cpp
I have even tried the following with the same result:
g++ -o my_program main.cpp -mcpu=cortex-a72 -mtune=cortex-a72
Any help will be very much appreciated
Statistics: Posted by fabianmejia — Sat Nov 02, 2024 2:53 pm