diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -193,6 +193,7 @@ bool ZExecstack; bool ZGlobal; bool ZHazardplt; + bool ZIfuncNoplt; bool ZInitfirst; bool ZInterpose; bool ZKeepTextSectionPrefix; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -358,7 +358,7 @@ static bool isKnownZFlag(StringRef S) { return S == "combreloc" || S == "copyreloc" || S == "defs" || S == "execstack" || S == "global" || S == "hazardplt" || - S == "initfirst" || S == "interpose" || + S == "ifunc-noplt" || S == "initfirst" || S == "interpose" || S == "keep-text-section-prefix" || S == "lazy" || S == "muldefs" || S == "nocombreloc" || S == "nocopyreloc" || S == "nodefaultlib" || S == "nodelete" || S == "nodlopen" || S == "noexecstack" || @@ -894,6 +894,7 @@ Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false); Config->ZGlobal = hasZOption(Args, "global"); Config->ZHazardplt = hasZOption(Args, "hazardplt"); + Config->ZIfuncNoplt = hasZOption(Args, "ifunc-noplt"); Config->ZInitfirst = hasZOption(Args, "initfirst"); Config->ZInterpose = hasZOption(Args, "interpose"); Config->ZKeepTextSectionPrefix = getZFlag( @@ -932,6 +933,9 @@ if (Config->SplitStackAdjustSize < 0) error("--split-stack-adjust-size: size must be >= 0"); + if (Config->ZText && Config->ZIfuncNoplt) + error("the -z text and -z ifunc-noplt options may not be combined"); + // Parse ELF{32,64}{LE,BE} and CPU type. if (auto *Arg = Args.getLastArg(OPT_m)) { StringRef S = Arg->getValue(); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1066,7 +1066,7 @@ // be resolved within the executable will actually be resolved that way at // runtime, because the main exectuable is always at the beginning of a search // list. We can leverage that fact. - if (!Sym.IsPreemptible && !Sym.isGnuIFunc()) { + if (!Sym.IsPreemptible && (!Sym.isGnuIFunc() || Config->ZIfuncNoplt)) { if (Expr == R_GOT_PC && !isAbsoluteValue(Sym)) Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr); else @@ -1094,6 +1094,14 @@ return; } + // We were asked not to generate PLT entries for ifuncs. Instead, pass the + // direct relocation on through. + if (Sym.isGnuIFunc() && Config->ZIfuncNoplt) { + Sym.ExportDynamic = true; + In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type); + return; + } + // Non-preemptible ifuncs require special handling. First, handle the usual // case where the symbol isn't one of these. if (!Sym.isGnuIFunc() || Sym.IsPreemptible) { diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1 --- a/lld/docs/ld.lld.1 +++ b/lld/docs/ld.lld.1 @@ -4,7 +4,7 @@ .\" .\" This man page documents only lld's ELF linking support, obtained originally .\" from FreeBSD. -.Dd February 26, 2019 +.Dd May 12, 2019 .Dt LD.LLD 1 .Os .Sh NAME @@ -546,6 +546,14 @@ .Dv DYNAMIC section. Different loaders can decide how to handle this flag on their own. +.It Cm ifunc-noplt +Do not emit PLT entries for ifunc symbols. +Instead, emit text relocations referencing the resolver. +This is an experimental optimization and only suitable for standalone +environments where text relocations do not have the usual drawbacks. +This option must be combined with the +.Fl z Li notext +option. .It Cm initfirst Sets the .Dv DF_1_INITFIRST diff --git a/lld/test/ELF/basic.s b/lld/test/ELF/basic.s --- a/lld/test/ELF/basic.s +++ b/lld/test/ELF/basic.s @@ -252,3 +252,6 @@ # RUN: not ld.lld %t --thinlto-jobs=0 2>&1 | FileCheck --check-prefix=NOTHREADSTHIN %s # RUN: not ld.lld %t --plugin-opt=jobs=0 2>&1 | FileCheck --check-prefix=NOTHREADSTHIN %s # NOTHREADSTHIN: --thinlto-jobs: number of threads must be > 0 + +# RUN: not ld.lld %t -z ifunc-noplt -z text 2>&1 | FileCheck --check-prefix=NOIFUNCPLTNOTEXTREL %s +# NOIFUNCPLTNOTEXTREL: the -z text and -z ifunc-noplt options may not be combined diff --git a/lld/test/ELF/gnu-ifunc-noplt-i386.s b/lld/test/ELF/gnu-ifunc-noplt-i386.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/gnu-ifunc-noplt-i386.s @@ -0,0 +1,69 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=i686-pc-freebsd %S/Inputs/shared2-x86-64.s -o %t1.o +// RUN: ld.lld %t1.o --shared -o %t.so +// RUN: llvm-mc -filetype=obj -triple=i686-pc-freebsd %s -o %t.o +// RUN: ld.lld -z ifunc-noplt -z notext --hash-style=sysv %t.so %t.o -o %tout +// RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM +// RUN: llvm-readobj -r --dynamic-table %tout | FileCheck %s + +// Check that we emitted relocations for the ifunc calls +// CHECK: Relocations [ +// CHECK-NEXT: Section (4) .rel.dyn { +// CHECK-NEXT: 0x401008 R_386_PLT32 bar +// CHECK-NEXT: 0x401003 R_386_PLT32 foo +// CHECK-NEXT: } +// CHECK-NEXT: Section (5) .rel.plt { +// CHECK-NEXT: 0x40300C R_386_JUMP_SLOT bar2 +// CHECK-NEXT: 0x403010 R_386_JUMP_SLOT zed2 +// CHECK-NEXT: } + +// Check that ifunc call sites still require relocation +// DISASM: Disassembly of section .text: +// DISASM-EMPTY: +// DISASM-NEXT: foo: +// DISASM-NEXT: 401000: c3 retl +// DISASM: bar: +// DISASM-NEXT: 401001: c3 retl +// DISASM: _start: +// DISASM-NEXT: 401002: e8 fc ff ff ff calll -4 +// DISASM-NEXT: 401007: e8 fc ff ff ff calll -4 +// DISASM-NEXT: 40100c: e8 1f 00 00 00 calll 31 +// DISASM-NEXT: 401011: e8 2a 00 00 00 calll 42 +// DISASM-EMPTY: +// DISASM-NEXT: Disassembly of section .plt: +// DISASM-EMPTY: +// DISASM-NEXT: .plt: +// DISASM-NEXT: 401020: ff 35 04 30 40 00 pushl 4206596 +// DISASM-NEXT: 401026: ff 25 08 30 40 00 jmpl *4206600 +// DISASM-NEXT: 40102c: 90 nop +// DISASM-NEXT: 40102d: 90 nop +// DISASM-NEXT: 40102e: 90 nop +// DISASM-NEXT: 40102f: 90 nop +// DISASM-EMPTY: +// DISASM-NEXT: bar2@plt: +// DISASM-NEXT: 401030: ff 25 0c 30 40 00 jmpl *4206604 +// DISASM-NEXT: 401036: 68 00 00 00 00 pushl $0 +// DISASM-NEXT: 40103b: e9 e0 ff ff ff jmp -32 <.plt> +// DISASM-EMPTY: +// DISASM-NEXT: zed2@plt: +// DISASM-NEXT: 401040: ff 25 10 30 40 00 jmpl *4206608 +// DISASM-NEXT: 401046: 68 08 00 00 00 pushl $8 +// DISASM-NEXT: 40104b: e9 d0 ff ff ff jmp -48 <.plt> + +.text +.type foo STT_GNU_IFUNC +.globl foo +foo: + ret + +.type bar STT_GNU_IFUNC +.globl bar +bar: + ret + +.globl _start +_start: + call foo@plt + call bar@plt + call bar2@plt + call zed2@plt diff --git a/lld/test/ELF/gnu-ifunc-noplt.s b/lld/test/ELF/gnu-ifunc-noplt.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/gnu-ifunc-noplt.s @@ -0,0 +1,66 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-freebsd %S/Inputs/shared2-x86-64.s -o %t1.o +// RUN: ld.lld %t1.o --shared -o %t.so +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %t.o +// RUN: ld.lld -z ifunc-noplt -z notext --hash-style=sysv %t.so %t.o -o %tout +// RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM +// RUN: llvm-readobj -r --dynamic-table %tout | FileCheck %s + +// Check that we emitted relocations for the ifunc calls +// CHECK: Relocations [ +// CHECK-NEXT: Section (4) .rela.dyn { +// CHECK-NEXT: 0x201008 R_X86_64_PLT32 bar 0xFFFFFFFFFFFFFFFC +// CHECK-NEXT: 0x201003 R_X86_64_PLT32 foo 0xFFFFFFFFFFFFFFFC +// CHECK-NEXT: } +// CHECK-NEXT: Section (5) .rela.plt { +// CHECK-NEXT: 0x203018 R_X86_64_JUMP_SLOT bar2 0x0 +// CHECK-NEXT: 0x203020 R_X86_64_JUMP_SLOT zed2 0x0 +// CHECK-NEXT: } + +// Check that ifunc call sites still require relocation +// DISASM: Disassembly of section .text: +// DISASM-EMPTY: +// DISASM-NEXT: foo: +// DISASM-NEXT: 201000: c3 retq +// DISASM: bar: +// DISASM-NEXT: 201001: c3 retq +// DISASM: _start: +// DISASM-NEXT: 201002: e8 00 00 00 00 callq 0 +// DISASM-NEXT: 201007: e8 00 00 00 00 callq 0 +// DISASM-NEXT: 20100c: e8 1f 00 00 00 callq 31 +// DISASM-NEXT: 201011: e8 2a 00 00 00 callq 42 +// DISASM-EMPTY: +// DISASM-NEXT: Disassembly of section .plt: +// DISASM-EMPTY: +// DISASM-NEXT: .plt: +// DISASM-NEXT: 201020: ff 35 e2 1f 00 00 pushq 8162(%rip) +// DISASM-NEXT: 201026: ff 25 e4 1f 00 00 jmpq *8164(%rip) +// DISASM-NEXT: 20102c: 0f 1f 40 00 nopl (%rax) +// DISASM-EMPTY: +// DISASM-NEXT: bar2@plt: +// DISASM-NEXT: 201030: ff 25 e2 1f 00 00 jmpq *8162(%rip) +// DISASM-NEXT: 201036: 68 00 00 00 00 pushq $0 +// DISASM-NEXT: 20103b: e9 e0 ff ff ff jmp -32 <.plt> +// DISASM-EMPTY: +// DISASM-NEXT: zed2@plt: +// DISASM-NEXT: 201040: ff 25 da 1f 00 00 jmpq *8154(%rip) +// DISASM-NEXT: 201046: 68 01 00 00 00 pushq $1 +// DISASM-NEXT: 20104b: e9 d0 ff ff ff jmp -48 <.plt> + +.text +.type foo STT_GNU_IFUNC +.globl foo +foo: + ret + +.type bar STT_GNU_IFUNC +.globl bar +bar: + ret + +.globl _start +_start: + call foo + call bar + call bar2 + call zed2