1

I have a jump table something like this:

jmp  rax

@@table:
jmp @@seg1
jmp @@sge2
jmp @@seg3
...

This was working perfectly with the understanding that the jmp code is two bytes in length. I have subsequently added code to the @@seg? code segments, which is now causing a crash when jumping to "further away" code segments (labels).

I'm concluding that the longer jumps are being encoded with a greater length than 2. Unfortunately the disassembler I use stops at the jmp rax line, so I can't confirm. But the conclusion is logical.

Is there a way to cause the compiler to align the jmp @@seg? commands at say 4 bytes so that I have a guaranteed large enough and consistent size for the jump table? If so, I could ensure rax increments in 4s.

I certainly don't want to start manually adding nop's in an attempt to solve this.

IamIC
  • 17,747
  • 20
  • 91
  • 154
  • 1
    Force all of them to be a near jump which have 5 byte size. You won't fit these into 4 bytes ;) Alternatively you could just store the addresses and use an indirect jump (if position independence is not a requirement). You could also store offsets but then you'd have to add a base yourself. – Jester Oct 08 '15 at 11:24
  • How do I force a near jump to a label? I don't really care about position dependence. I just want to be able to jump to the labeled segments. What's the easiest way to store the addresses of each segment for look-up? – IamIC Oct 08 '15 at 11:29
  • For storing the addresses, use whatever directive your assembler supports such as `dq @@seg1`. As for forcing a near jump, you probably do something like `jmp near @@seg1` – Jester Oct 08 '15 at 11:47
  • Unfortunately, the compiler says it's ignoring the near directive. Would you mind giving me an example of how to store the address then jump to it? – IamIC Oct 08 '15 at 11:53
  • Storing the address, I already have. Jumping is as simple as `jmp [rax]`. – Jester Oct 08 '15 at 11:54
  • I know this will work: `lea r8, [rip+@@seg1] ; jmp r8` but it seems heavy and will have latency. – IamIC Oct 08 '15 at 11:55

0 Answers0