0

I am interested in compiling / linking an assembly project whose shared library function calls bypass the PLT (use the GOT directly). I Tried using -fno-plt but the resulting library calls still go through the PLT.

Questions

I am wondering:

  1. Why does the -fno-plt argument not work when compiling assembly?
  2. Is there a way to compile assembly so that shared library function calls bypass the PLT?

Assembly PLT Bypass with -fno-plt NOT Working.

Using the simplest example:

    .global main
    .text
main:
    callq   exit

When compiled with:

gcc -fno-plt test0.S -o test0

Produces the following for main:

0000000000001139 <main>:
    1139:   e8 f2 fe ff ff          callq  1030 <exit@plt>
    113e:   66 90                   xchg   %ax,%ax

Which is calling exit through the PLT.

C PLT Bypass with -fno-plt Working

Alternatively the same code in C:

extern void exit(int);
int main() {
    exit(0);
}

Compiled with:

gcc -O2 -fno-plt  test1.c -o test1 

Gets the following for main:

0000000000001040 <main>:
    1040:   f3 0f 1e fa             endbr64 
    1044:   50                      push   %rax
    1045:   58                      pop    %rax
    1046:   31 ff                   xor    %edi,%edi
    1048:   48 83 ec 08             sub    $0x8,%rsp
    104c:   ff 15 96 2f 00 00       callq  *0x2f96(%rip)        # 3fe8 <exit@GLIBC_2.2.5>
    1052:   66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
    1059:   00 00 00 
    105c:   0f 1f 40 00             nopl   0x0(%rax)

Which correctly bypasses the PLT for the call to exit.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Noah
  • 1,647
  • 1
  • 9
  • 18
  • 3
    If you use `gcc -S` you will see what the compiler generates to make it work. TL;DR: `call *exit@GOTPCREL(%rip)`. Note that `-fno-plt` only affects code generation it is not needed for your assembly. – Jester Jan 25 '22 at 02:37
  • 4
    `-fno-plt` has zero effect on the assembler or linker behaviour, only on the C->asm translation step which doesn't happen when you give GCC a `.s` file. GCC code-gen options won't re-write your asm. Same reason why "recompile with -fPIC" error messages only apply to object files generated from C, not hand-written asm. ([32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427)) – Peter Cordes Jan 25 '22 at 02:40
  • 2
    Also related: [Can't call C standard library function on 64-bit Linux from assembly (yasm) code](https://stackoverflow.com/q/52126328) mentions the AT&T syntax for it way down in the last section, about relocations that are relaxable or not. [x86\_64: Is it possible to "in-line substitute" PLT/GOT references?](https://stackoverflow.com/q/10849308) shows the compiler-generated asm and talks about `-fno-plt`, but maybe not an exact duplicate, IDK. Maybe of that and one about how "recompile with `-fPIC`" doesn't mean that option will fix asm that uses 32-bit abs addr modes. – Peter Cordes Jan 25 '22 at 03:04

0 Answers0