How to map machine instctions in memory and execute them? (Aka, how to create a loader)
Johan
j at j.nl
Mon Jun 6 18:05:23 UTC 2022
On Monday, 6 June 2022 at 15:13:45 UTC, rempas wrote:
> ```
> // mov rdx, <wordLen>
> *cast(char*)(code + 14) = 0x48;
> *cast(char*)(code + 15) = 0xC7;
> *cast(char*)(code + 16) = 0xC2;
> *cast(char*)(code + 17) = 12;
> *cast(char*)(code + 18) = 0x00;
> *cast(char*)(code + 19) = 0x00;
> *cast(char*)(code + 20) = 0x00;
>
> // mov rdx, <location where data are allocated>
> *cast(char*)(code + 21) = 0x48;
> *cast(char*)(code + 22) = 0xC7;
> *cast(char*)(code + 23) = 0xC1;
> *cast(long*)(code + 24) = cast(long)data;
> *cast(char*)(code + 32) = 0x00;
> ```
This instruction is wrong. Note that you are writing twice to
RDX, but also that you are using `mov sign_extend imm32, reg64`
instead of `mov imm64, reg64` (`0x48 0xBA`?). Third, why append
an extra zero (`*cast(char*)(code + 32) = 0x00;`)? That must be a
bug too.
cheers,
Johan
More information about the Digitalmars-d-learn
mailing list