Author Topic: Relocation for PIC code in NASM 64-bit  (Read 15579 times)

nobody

  • Guest
Relocation for PIC code in NASM 64-bit
« on: July 26, 2008, 12:30:36 AM »
GAS supports RIP-relative reference to a linker-generated symbol pointer.

example code from "AMD64 ABI Draft 0.99":

C source                               Assembly Code

extern int  src[655356];           .extern src
    extern int  dst[65536];            .extern dst
    extern int *ptr;                   .extern ptr

static int  lsrc [65536];          .local lsrc
    static int  ldst [65536];          .comm  lsrc, 2621444,4
    static int *lptr;                  .local ldst
                                       .comm  ldst, 2621444,4
                                       .local lptr
                                       .comm  lptr,8,8
                                       .text
    dst[0] = src[0];                    movq  src@GOTPCREL(%rip), %rax
                                        movl  (%rax), %edx
                                        movq  dst@GOTPCREL(%rip), %rax
                                        movl  %edx, (%rax)

ptr = dst;                          movq  ptr@GOTPCREL(%rip), %rax
                                        movq  dst@GOTPCREL(%rip), %rdx
                                        movq  %rdx, (%rax)

*ptr = src[0];                      movq  ptr@GOTPCREL(%rip), %rax
                                        movq  (%rax) %rdx
                                        movq  src@GOTPCREL(%rip), %rax
                                        movl  (%rax), %eax
                                        movl  %eax, (%rdx)

ldst[0] = lsrc[0];                  movl  lsrc(%rip), %eax
                                        movl  %eax, ldst(%rip)

lptr = ldst;                        lea   ldst(%rip), %rdx
                                        movq  %rdx, lptr(%rip)

*lptr = lsrc[0];                    movq  lptr(%rip), %rax
                                        movl  lsrc(%rip), %edx
                                        movl  %edx, (%rax)


Yasm supports RIP-relative addressing as well and "wrt ..gotpcrel" is used for this purpose.
It seems that NASM does not recognize "rip" and does not support the "wrt ..gotpcrel" relocation type, either.
Is there any way to do the RIP-relative addressing like this in NASM 64-bit?

nobody

  • Guest
Re: Relocation for PIC code in NASM 64-bit
« Reply #1 on: July 26, 2008, 12:37:13 AM »
Oops... for some reason, it got totally messed up when I posted my question. I retype the code to make it easier to read.

< C source >
extern int  src[655356];
extern int  dst[65536];
extern int *ptr;                  

static int  lsrc [65536];
static int  ldst [65536];
static int *lptr;                

dst[0] = src[0];
ptr = dst;
*ptr = src[0];
ldst[0] = lsrc[0];
lptr = ldst;
*lptr = lsrc[0];                  


< Assembly code >
.extern src
.extern dst
.extern ptr

.local lsrc
.comm  lsrc, 2621444,4
.local ldst
.comm  ldst, 2621444,4
.local lptr
.comm  lptr,8,8
.text
 movq  src@GOTPCREL(%rip), %rax
 movl  (%rax), %edx
 movq  dst@GOTPCREL(%rip), %rax
 movl  %edx, (%rax)

movq  ptr@GOTPCREL(%rip), %rax
 movq  dst@GOTPCREL(%rip), %rdx
 movq  %rdx, (%rax)

movq  ptr@GOTPCREL(%rip), %rax
 movq  (%rax) %rdx
 movq  src@GOTPCREL(%rip), %rax
 movl  (%rax), %eax
 movl  %eax, (%rdx)

movl  lsrc(%rip), %eax
 movl  %eax, ldst(%rip)

lea   ldst(%rip), %rdx        

movq  lptr(%rip), %rax
 movl  lsrc(%rip), %edx
 movl  %edx, (%rax)


I would like to know how I can do the same thing in NASM 64-bit.
Help me!!

Offline H. Peter Anvin

  • NASM Developer
  • Jr. Member
  • *****
  • Posts: 18
Re: Relocation for PIC code in NASM 64-bit
« Reply #2 on: October 02, 2008, 05:47:45 AM »
The syntax is:

mov rax,[rel foo wrt ..got]

(with the "rel" bit optional if you specify "default rel".)

However, I just tested it and found that it doesn't actually work right.  Instead we get the bogus error:

gotpcrel.asm:3: error: ELF format cannot produce PC-relative GOT references

I'll put that on the short list of things to do.

Offline H. Peter Anvin

  • NASM Developer
  • Jr. Member
  • *****
  • Posts: 18
Re: Relocation for PIC code in NASM 64-bit
« Reply #3 on: October 02, 2008, 05:48:59 AM »
This would have better been filed as a bug report... we'd probably have seen it sooner.

Offline kolargol

  • New Member
  • Posts: 1
Re: Relocation for PIC code in NASM 64-bit
« Reply #4 on: March 20, 2020, 09:32:10 PM »
i know i am late, 12 years later lol, but could you provide more translation from the gas code with the same variable name because there is no foo declared in the c source or gas  source making it a little hard to know which operation you refer to.

Thanks