0

How to load an absolute address to register via lea?
I tried this code:

asm.lea(asmjit::x86::rax, (uint64_t) (address));

And I try to use this code

asm.lea(asmjit::x86::rax, asmjit::x86::ptr((uint64_t) (address)));

But all of them don't work. Asmjit logged "Invalid use an 64-bit pointer". But if I code this code in flat assembler, it works (lea rax, QWORD_PTR [address]).

zx485
  • 28,498
  • 28
  • 50
  • 59
  • 2
    `lea` cannot load an absolute 64-bit address in general. It cannot be encoded. 32-bit absolute (sign-extended), or RIP-relative, but not 64-bit absolute. – harold Jun 07 '23 at 20:57
  • 1
    Do you want a RIP-relative LEA, like NASM `lea rax, [rel label_name]`? That's the normal way to get the address of constants and static/global variables, e.g. for string literals. So if you already know where your data is, and it's within 2GiB of the code, you can and should use RIP-relative instead of 10-byte `mov r64, imm64`. ([How to load address of function or label into register](https://stackoverflow.com/q/57212012)). Hopefully asmjit has special support for generating rel32 operands, since it has to be relative to the end of the instruction. – Peter Cordes Jun 07 '23 at 21:01
  • But how, then, do we pass the addresses in the function parameters to the __fastcall declaration? They are 64-bit and parameters are in registers(The first four) – Unbeliviable Jun 07 '23 at 21:15
  • Addresses of what? Of locations that are within +- 2GiB of the code? If so, then you want a RIP-relative LEA. (Or if the data is in the low 32 bits of virtual address space, `mov ecx, imm32`, like when compiling a Linux program with `-fno-pie`. In that case it's an absolute address so the JIT buffer can be far from the data.) If neither of those apply, then ideally change your memory allocation so they will, but at worse you can always use `mov r64, imm64` like I said. – Peter Cordes Jun 07 '23 at 21:25
  • Similar problem to [Handling calls to (potentially) far away ahead-of-time compiled functions from JITed code](https://stackoverflow.com/q/54947302) which has some suggestions for getting JIT buffers close to each other. – Peter Cordes Jun 07 '23 at 21:26
  • Once you put the value into a 64-bit register, like `rax`, it is full 64-bits wide — just maybe some of the bits are zeros. – Erik Eidt Jun 07 '23 at 23:48

0 Answers0