smwikipedia
smwikipedia

Reputation: 64173

Is it possible to build C source code written for ARM to run on x86 platform?

I got some source code in plain C. It is built to run on ARM with a cross-compiler on Windows.

Now I want to do some white-box unit testing of the code. And I don't want to run the test on an ARM board because it may not be very efficient.

Since the C source code is instruction set independent, and I just want to verify the software logic at the C-level, I am wondering if it is possible to build the C source code to run on x86. It makes debugging and inspection much easier.

Or is there some proper way to do white-box testing of C code written for ARM?

Thanks!

BTW, I have read the thread: How does native android code written for ARM run on x86?

It seems not to be what I need.

ADD 1 - 10:42 PM 7/18/2021

The physical ARM hardware that the code targets may not be ready yet. So I want to verify the software logic at a very early phase. Based on John Bollinger's answer, I am thinking about another option: Just build the binary as usual for ARM. Then use QEMU to find a compatible ARM cpu to run the code. The code is assured not to touch any special hardware IO. So a compatible cpu should be enough to run all the code I think. If this is possible, I think I need to find a way to let QEMU load my binary on a piece of emulated bare-metal. And to get some output, I need to at least write a serial port driver to bridge my binary to the serial port.

ADD 2 - 8:55 AM 7/19/2021

Some more background, the C code is targeting ARMv8 ISA. And the code manipulates some hardware IPs which are not ready yet. I am planning to create a software HAL for those IPs and verify the C code over the HAL. If the HAL is good enough, everything can be purely software and I guess the only missing part is a ARMv8 compatible CPU, which I believe QEMU can provide.

ADD 3 - 11:30 PM 7/19/2021

Just found this link. It seems QEMU user mode emulation can be leveraged to run ARM binaries directly on a x86 Linux. Will try it and get back later.

ADD 4 - 11:42 AM 7/29/2021

An some useful links:

Override a function call in C

__attribute__((weak)) and static libraries

What are weak functions and what are their uses? I am using a stm32f429 micro controller

Why the weak symbol defined in the same .a file but different .o file is not used as fall back?

Upvotes: 1

Views: 840

Answers (2)

Marc
Marc

Reputation: 1189

If you were writing ARM code for a windows desktop application there would be no difference for the most part and the code would just compile and run. My guess is you are developing for some device that does some specific task.

I do this for a lot of my embedded ARM code. Typically the core algorithms work just fine when built on x86 but the whole application does not. The problems come in with the hardware other than the CPU. For example I might be using a LCD display, some sensors, and Free RTOS on the ARM project but the code that runs on Windows does not have any of these. What I do is extract important pieces of C/C++ code and write a test framework around it. In the real ARM code the device is reading values from a sensor and doing something with it. In the test code that runs on a desktop the code reads from a data file with fake sensor values and writes its output to a datafile that can be analyzed. This way I can have white box tests for the most complicated code.

May I ask, roughly what does this code do? An ARM processor with no peripherals would be kind of useless. Typically we use the processor to interact with some other hardware like a screen, some buttons, or Bluetooth. It's those interactions that are going to be the most problematic.

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180048

Now I want to do some white-box unit testing of the code. And I don't want to run the test on an ARM board because it may not be very efficient.

What does efficiency have to do with it if you cannot be sure that your test results are representative of the real target platform?

Since the C source code is instruction set independent,

C programs vary widely in how portable they are. This tends to be less related to CPU instruction set than to target machine and implementation details such as data type sizes, word endianness, memory size, and floating-point implementation, and implementation-defined and undefined program behaviors.

It is not at all safe to assume that just because the program is written in C, that it can be successfully built for a different target machine than it was developed for, or that if it is built for a different target, that its behavior there is the same.

I am wondering if it is possible to build the C source code to run on x86. It makes debugging and inspection much easier.

It is probably possible to build the program. There are several good C compilers for various x86 and x86_64 platforms, and if your C code conforms to one of the language specifications then those compilers should accept it. Whether the behavior of the result is representative of the behavior on ARM is a different question, however (see above).

It may nevertheless be a worthwhile exercise to port the program to another platform, such as x86 or x86_64 Windows. Such an exercise would be likely to unmask some bugs. But this would be a project in its own right, and I doubt that it would be worth the effort if there is no intention to run the program on the new platform other than for testing purposes.

Or is there some proper way to do white-box testing of C code written for ARM?

I don't know what proper means to you, but there is no substitute for testing on the target hardware that you ultimately want to support. You might find it useful to perform initial testing on emulated hardware, however, instead of on a physical ARM device.

Upvotes: 2

Related Questions