[LLVMdev] implementing PIC for linux x86-64

Rafael Espindola espindola at google.com
Fri May 30 06:13:00 PDT 2008


Some comments from what I have seen so far:

On linux X86_64, calls to local but externally visible functions
should use the PLT. Access to local (same compilation unit), variables
can just use RIP relative access.

For example
-------------
void f(void);
int g(void) {
  f();
  return 0;
}
static int a;
int h(void) {
  return a;
}
---------

Is compiled by GCC to

------------------------
g:
        subq    $8, %rsp
        call    f at PLT
        xorl    %eax, %eax
        addq    $8, %rsp
        ret

h:
        movl    a(%rip), %eax
        ret

------------------------

LLVM currently uses PICStyle to decide how to handle PIC. If it is
RIPRel, the call doesn't use PLT. If it is GOT, the local variable
access doesn't use RIP relative addressing.

Looks like we need a second variable (say PICCallStyle). With it we
would have something like:

  else if (Subtarget.isTargetELF()) {
    Subtarget.setPICCallStyle(PICCallStyle::PLT);
    if (Subtarget.is64Bit()) {
      Subtarget.setPICStyle(PICStyle::RIPRel);
    } else {
      Subtarget.setPICStyle(PICStyle::GOT);
    }
  }

Comments?

Thanks,
-- 
Rafael Avila de Espindola

Google Ireland Ltd.
Gordon House
Barrow Street
Dublin 4
Ireland

Registered in Dublin, Ireland
Registration Number: 368047



More information about the llvm-dev mailing list