OmriR
OmriR

Reputation: 13

Does QEMU have restriction on executing a command from DATA section?

I'm using QEMU to simulate an ARM11 CPU.

My program is too complicated to explain here, so i'll project the issue into a simpler program. So my program contains 2 c files:

I'm compiling some_code.c (into some_code.o) and then i convert it to an HEX array variable, which represents the code of some_code.c.

Now i'm linking both object files (main.o & some_code.o). This variable (HEX array variable) is located at the DATA segment.

Now I’m calling from the code in main.c to the HEX array variable (my intention is that at this point the code of some_code.c will start executing). When the Program Counter (PC) reach the HEX array variable, it gets an exception (i don't have more details about the exception).

If i copy this HEX array variable from DATA section to CODE section, now when the PC reach this line, it is successfully able to step it without exception.

So my questions are:

Thanks in advanced,

Omri

Upvotes: 1

Views: 343

Answers (2)

If I understand your description correctly, then you aren't running into a restriction of QEMU, but into a restriction of the CPU that it's emulating. QEMU doesn't know anything about data sections and code sections, but the operating system that you run in QEMU does.

Most OSes set up the code and data sections with different permissions: code is normally readable and executable but not writable, read-only data is readable but not executable or writable, and mutable data is readable and writable but not executable.

The CPU enforces read, write and execution permissions through flags in the MMU descriptors. On ARM, the execution permission is controlled by the XN bit in the page descriptor, present since ARMv6.

If you want to have executable data (for example for a just-in-time compiler, or a dynamic code loading mechanism), you need to figure out how to instruct your operating system to make memory executable.

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

It will be a combination of the linker and the operating system. It is likely that the linker marks the data section as "data" and the loader will then create an area of memory without execute privilege on it to contain the data. This is a feature of the hardware QEMU is emulating, not QEMU itself i.e. if you were running this on a real machine, you would see the same problem.

It will be possible to change the data section to be executable, but the details will depend on which OS you are running and what compiler toolchain you are using. Any interpreter that has a JIT compiler must do something similar.

Note that, in general, it is considered to be bad practice to make the data section executable because that can lead to all sorts of security exploits.

Upvotes: 1

Related Questions