RIP Relative Addressing

시갈 2013.10.13 01:19 조회 수 : 1899 추천:1

IA-32e Mode RIP Relative Addressing.


비교를 위하여


1. absolute addressing


01   .text

02       .global _start

03

04   _start:

05       mov $0xd, %rdx

06

07       mov $msg, %rsi

08       pushq $0x1

09       pop %rax

10       mov %rax, %rdi

11       syscall

12

13       xor %rdi, %rdi

14       pushq $0x3c

15       pop %rax

16       syscall

17

18   .data

19   msg:

20       .ascii    "Hello world!n"


=============================================================================

2. RIP Relative addressing.


01   .text

02       .global _start

03

04   _start:

05       mov $0xd, %rdx

06

07       leaq msg(%rip), %rsi

08       pushq $0x1

09       pop %rax

10       mov %rax, %rdi

11       syscall

12

13       xor %rdi, %rdi

14       pushq $0x3c

15       pop %rax

16       syscall

17

18   msg:

19       .ascii    "Hello world!n"

=============================================================================


2개의 프로그램은 동일한 기능을 합니다.

차이가 나는 곳은 7 line인데 absolute addressing의 경우는 대상 data가 .data section에 존재합니다.

absolute addressing 예제 18 line에서 .data section 지정함.


그러나 RIP Relative addressing에서는 동일하게 .text section에서 대상 data를 RIP Relative addressing을

이용하여 data를 지정함(7 line).

정확한 분석을 위하여 해당 프로그램을 컴파일하여 objdump하면

objdump -d hello  (컴파일 실행파일명을 hello)


0000000000400078 <.text>:

  400078: 48 c7 c2 0d 00 00 00  mov    $0xd,%rdx

  40007f: 48 8d 35 10 00 00 00  lea    0x10(%rip),%rsi        # 0x400096

  400086: 6a 01                        pushq  $0x1

  400088: 58                            pop    %rax

  400089: 48 89 c7                   mov    %rax,%rdi

  40008c: 0f 05                        syscall 

  40008e: 48 31 ff                    xor    %rdi,%rdi

  400091: 6a 3c                       pushq  $0x3c

  400093: 58                           pop    %rax

  400094: 0f 05                       syscall 

  400096: 48                          rex.W                                           // 여기서부터 msg로 Hello Worldn

  400097: 65                          gs

  400098: 6c                          insb   (%dx),%es:(%rdi)

  400099: 6c                          insb   (%dx),%es:(%rdi)

  40009a: 6f                           outsl  %ds:(%rsi),(%dx)

  40009b: 20 77 6f                  and    %dh,0x6f(%rdi)

  40009e: 72 6c                      jb     0x40010c

  4000a0: 64 21 0a                 and    %ecx,%fs:(%rdx)

  

  7 line의 leaq msg(%rip), %rsi가 lea 0x10(%rip),%rsi로 변환됨을 알수 있다.

  7 line 실행시 rip는 다음 실행번지인 0x400086가 들어있으며 msg가 0x10으로

  변환되어 rsi에는 0x400086 + 0x10 = 0x400096가 저장된다.

  여기서 중요한것은 msg가 0x10으로 변환된것인데 이값은 rip와 실제 data가

  들어있는 어드레스의 차이값이 0x10이기 때문이다.

  실제로 0x400096부터 Hello world!n가 들어있다.

  즉 RIP Relative Addressing은 RIP와 지정할 어드레스까지의 옵셋으로 지정한다.

  

  

XE Login