Author Topic: RIP Register  (Read 31322 times)

Offline ironman

  • Jr. Member
  • *
  • Posts: 5
RIP Register
« on: September 01, 2011, 09:17:22 PM »
Hello,

I am trying to assemble a file that contains code which directly references the RIP register (note, I did not write this code, I am trying to use existing assembly). Here is an example (intel syntax):

Code: [Select]
addsd  xmm0, [rip+0x484]
 
However, whenever I reference RIP, I get
Quote
error: symbol `rip' undefined
I checked and R_RIP isn't even defined in the regs.h file like R_RAX or R_RBP. I don't see why it shouldn't be though? How can I get NASM to handle code that directly references the RIP register? Thanks.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: RIP Register
« Reply #1 on: September 01, 2011, 09:48:15 PM »
I have no experience with 64-bit code! As a WAG, try...

Code: [Select]
addsd xmm0, [rel $ +0x484]

If that doesn't do what the original code intends, get back to us.

Best,
Frank


Offline ironman

  • Jr. Member
  • *
  • Posts: 5
Re: RIP Register
« Reply #2 on: September 01, 2011, 11:56:33 PM »
Thanks Frank! That seems to work. Is there a reason NASM doesn't support this code syntax by default?


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: RIP Register
« Reply #3 on: September 02, 2011, 02:20:44 PM »
I don't know. We can't use IP or EIP either. For "consistency" maybe? What assembler is it that supports the syntax you showed?

Best,
Frank


Offline ironman

  • Jr. Member
  • *
  • Posts: 5
Re: RIP Register
« Reply #4 on: September 02, 2011, 02:46:09 PM »
GAS

Code: [Select]
.intel_syntax
addsd %xmm0, [%rip + 0x484]

works fine.

-Matt

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: RIP Register
« Reply #5 on: September 02, 2011, 03:25:17 PM »
This is x64 RIP-relative addressing.  The RIP register is simply used as a base register to access offsets relative to it's position and used for Position Independent Code (PIC).

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: RIP Register
« Reply #6 on: September 02, 2011, 03:36:50 PM »
Okay, thanks Matt! Nasm is not, strictly speaking "Intel syntax". Claims to be "similar to Intel's, but less complex". I'm not sure it's actually "less complex" in this particular case... and maybe not that "similar", either. For now, "that's the way it is".

We can do:

Code: [Select]
%idefine rip rel $

(the 'i' in "%idefine" makes it case-insensitive) That works in this case, but not if it were a label (which is probably more common?)

As an alternative to putting "rel" in the brackets, we can do:

Code: [Select]
default rel

... and it will apply throughout. However, we still need the '$' (unless it's a label), or Nasm burps up a warning... and emits different code. Since a label is probably more common, I hope this won't be too much of a problem.

Best,
Frank


Offline David Cooper

  • Jr. Member
  • *
  • Posts: 9
Re: RIP Register
« Reply #7 on: September 02, 2011, 05:43:04 PM »
However, whenever I reference RIP, I get
Quote
error: symbol `rip' undefined
I checked and R_RIP isn't even defined in the regs.h file like R_RAX or R_RBP. I don't see why it shouldn't be though? How can I get NASM to handle code that directly references the RIP register? Thanks.

There are no machine code instructions to let you access IP/EIP/RIP directly, so there is nothing missing in assembler. If you ever need to find out what address IP/EIP/RIP holds you have to call a routine designed to examine the ret address on the stack.
« Last Edit: September 02, 2011, 05:45:31 PM by David Cooper »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: RIP Register
« Reply #8 on: September 02, 2011, 06:43:44 PM »
There are no machine code instructions to let you access IP/EIP/RIP directly, so there is nothing missing in assembler.

Exactly. Referencing the RIP register, while somewhat intuitive, was a hasty language design choice in other assemblers. The distinction is simply absolute, or relative.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: RIP Register
« Reply #9 on: September 02, 2011, 07:24:25 PM »
Code: [Select]
bits 64

default rel
...
lea rax, [$]
...

Would this tell me current RIP without doing a call/pop?

(MUST upgrade my hardware!)

Best,
Frank


Offline David Cooper

  • Jr. Member
  • *
  • Posts: 9
Re: RIP Register
« Reply #10 on: September 03, 2011, 06:03:17 PM »
Code: [Select]
bits 64

default rel
...
lea rax, [$]
...

Would this tell me current RIP without doing a call/pop?

(MUST upgrade my hardware!)

Best,
Frank

Code: [Select]
lea eax, [$]
That works perfectly in 32-bit mode - thanks for the tip!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: RIP Register
« Reply #11 on: September 03, 2011, 06:36:46 PM »
Heh! How about that? I never thought to try it in 32-bit code. I'm guessing that for Position Independent Code, it may not work. I'm guessing that if I tell ld "-shared", it's going to yell at me. In 32-bit code, '$' is a "relocatable value". In 64-bit code, with RIP-relative addressing, maybe not. 64-bit code is... different(!).

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: RIP Register
« Reply #12 on: September 04, 2011, 01:25:28 AM »
When in doubt, look at the generated opcodes...

Code: [Select]
;nasm -f bin -o test32.o test32.asm
[BITS 32]
lea eax,[$]

Code: [Select]
;ndisasm -b32 test32.o
00000000  8D0500000000      lea eax,[dword 0x0]

Code: [Select]
;nasm -f bin -o test64.o test64.asm
[BITS 64]
lea eax,[abs $]
lea eax,[rel $]
lea rax,[abs $]
lea rax,[rel $]

Code: [Select]
;ndisasm -b64 test64.o
00000000  8D042500000000    lea eax,[0x0]
00000007  8D05FAFFFFFF      lea eax,[rel 0x7]
0000000D  488D04250D000000  lea rax,[0xd]
00000015  488D05F9FFFFFF    lea rax,[rel 0x15]
« Last Edit: September 04, 2011, 05:04:05 AM by Keith Kanios »