Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm a bit confused by how gcc encodes relative jumps. I have the following:

int main(void)
    __asm__ __volatile__(
        "jmp label\n"
        "label:\n"
        "nop\n"
    return 0;

Building this (gcc -c -o test.o test.c) shows the following (objdump -M intel -d test.o):

0000000000000000 <main>:
   0:   55                      push   rbp
   1:   48 89 e5                mov    rbp,rsp
   4:   eb 00                   jmp    6 <label>
0000000000000006 <label>:
   6:   90                      nop

rasm2 -d eb00 shows jmp 2, which means the jump is being performed with an offset of 2. Now, I had understood that relative jumps' offsets are added to the current value of eip, which should be pointing at the next instruction (i.e. nop). This encoding makes me think that the offset is relative to the address of the jmp itself. Shouldn't the jmp be encoded as jmp 0, since nop is already at label?

If that's true, that's extremely confusing. I'm surprised that even radare would display the offset as 2. – Martin Feb 17, 2019 at 5:20 @Martin wouldn't it be more confusing if radare decided to do it different from all the other tools? – harold Feb 17, 2019 at 5:24 I don't know. Maybe radare should include an option to show the real offsets. In any case, this seems to clear things up for me. Thanks, harold – Martin Feb 17, 2019 at 5:34 @martin Normally disassemblers display the absolute address of the destination for the operand of jump instructions. I would've expected rasm2 to display jmp 6 just like objdump did. It's possible that rasm2 thinks that the JMP instruction is at address 0 for whatever reason. – Ross Ridge Feb 17, 2019 at 5:58 @MikkoRantalainen the compiler had no choice in this case because inline asm was used to create that useless jump – harold Feb 26, 2022 at 10:38

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.