Menu

Relocation for PIC code in NASM 64-bit

2008-07-26
2013-06-04
  • Nobody/Anonymous

    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/Anonymous

      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!!

       
    • H. Peter Anvin

      H. Peter Anvin - 2008-10-02

      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.

       
    • H. Peter Anvin

      H. Peter Anvin - 2008-10-02

      This would have better been filed as a bug report... we'd probably have seen it sooner.

       
    • H. Peter Anvin

      H. Peter Anvin - 2008-12-10

      Code for this is in 2.06rc1.

       

Log in to post a comment.