Question

Let's say I have a malloc-ed variable and I want to move it's address to %rdi, all that generating the opcode from C. It should look to something like this:

unsigned char op_1[] = { 0x48, 0x8B, 0x3C, 0x25 }; //movq
unsigned char *a = malloc(1); // Let's asume a is now at 0x1234567812345678
unsigned char *bytecode = malloc(sizeof(op_1) + sizeof(void *)); // 12 bytes
memcpy(bytecode, op_1, sizeof(op_1)); // 4 bytes
memcpy(bytecode + sizeof(op_1), &a, sizeof(void *)); // 8 bytes

That should leave the content of bytecode to: 48 8B 3C 25 78 56 34 12 78 56 34 12. Anyways, that is invalid bytecode, as (if I'm not misreading the docs) 48 8B 3C 25 can be followed by 4 bytes for the address.

My question is how am I supposed to move an 8 bytes long address to %rdi?

Was it helpful?

Solution

It looks like the right opcode is 48 bf. I compile the following assembly (using Intel syntax and nasm):

BITS 64
        mov rdi, 0x1234567812345678

Then I pass the result through objdump -d -w -Mintel:

0000000000000000 <.text>:
   0:   48 bf 78 56 34 12 78 56 34 12   movabs rdi,0x1234567812345678

The opcode 48 bf is composed of the REX.W prefix 48 to designate 64-bit operands, the opcode b8 +rd io, and the register selector for RDI (111):

MOV r64, imm64: Move imm64 to r64

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top