[Bug middle-end/100593] [ELF] -fno-pic: Use GOT to take address of an external default visibility function

i at maskray dot me gcc-bugzilla@gcc.gnu.org
Tue May 18 17:47:44 GMT 2021


--- Comment #8 from Fangrui Song <i at maskray dot me> ---
Seems that -fno-plt -fno-pic does have the required properties.
A side effect is that all external calls use the   (x86-64) call
*f@GOTPCREL(%rip) (x86-32) call *f@GOT  form.

The instruction is one byte longer. (Calling a function is a common case.
Taking the address in a non-vtable case is uncommon. So I'd rather punish the
uncommon address taking).
When the linker notices that the branch target is defined in the executable, it
can optimize out the GOT to use an addr32 prefix instead.
(gold and ld.lld haven't implemented the optimization for 32-bit)

int f();
void h() {}

void *g()
  h();       // call h
  f();       // call *f@GOTPCREL(%rip)
  return f;  // movq f@GOTPCREL(%rip), %rax

More information about the Gcc-bugs mailing list