Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -39,6 +39,7 @@ R_PPC_OPD, R_PPC_PLT_OPD, R_PPC_TOC, + R_X86_RELAX_GOT_PC, R_RELAX_TLS_GD_TO_IE, R_RELAX_TLS_GD_TO_IE_PC, R_RELAX_TLS_GD_TO_LE, Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -228,6 +228,7 @@ return SymVA - P; } case R_PC: + case R_X86_RELAX_GOT_PC: return Body.getVA(A) - P; case R_PAGE_PC: return getAArch64Page(Body.getVA(A)) - getAArch64Page(P); @@ -272,6 +273,13 @@ write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1) } + if (Expr == R_X86_RELAX_GOT_PC) { + if (SymVA + 0x80000000 > 0xFFFFFFFF) + error("R_X86_64_GOTPCREL[X] relaxation overflow"); + BufLoc[-2] = 0x8d; + Type = R_X86_64_PC32; + } + Target->relocateOne(BufLoc, Type, SymVA); } } Index: ELF/Target.h =================================================================== --- ELF/Target.h +++ ELF/Target.h @@ -69,7 +69,8 @@ virtual void writeThunk(uint8_t *Buf, uint64_t S) const {} - virtual RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const = 0; + virtual RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const = 0; virtual void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const = 0; virtual bool isGotRelative(uint32_t Type) const; bool canRelaxTls(uint32_t Type, const SymbolBody *S) const; Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -70,7 +70,8 @@ class X86TargetInfo final : public TargetInfo { public: X86TargetInfo(); - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; void writeGotPltHeader(uint8_t *Buf) const override; uint32_t getDynRel(uint32_t Type) const override; @@ -102,7 +103,8 @@ class X86_64TargetInfo final : public TargetInfo { public: X86_64TargetInfo(); - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; uint32_t getDynRel(uint32_t Type) const override; uint32_t getTlsGotRel(uint32_t Type) const override; bool pointsToLocalDynamicGotEntry(uint32_t Type) const override; @@ -126,6 +128,9 @@ void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; + +private: + bool canRelaxGotPCRel(const SymbolBody &S, const uint8_t *Loc) const; }; class PPCTargetInfo final : public TargetInfo { @@ -133,13 +138,15 @@ PPCTargetInfo(); void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; bool isRelRelative(uint32_t Type) const override; - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; }; class PPC64TargetInfo final : public TargetInfo { public: PPC64TargetInfo(); - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, int32_t Index, unsigned RelOff) const override; bool needsGot(uint32_t Type, const SymbolBody &S) const override; @@ -151,7 +158,8 @@ class AArch64TargetInfo final : public TargetInfo { public: AArch64TargetInfo(); - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; uint32_t getDynRel(uint32_t Type) const override; bool isTlsGlobalDynamicRel(uint32_t Type) const override; bool isTlsInitialExecRel(uint32_t Type) const override; @@ -177,13 +185,15 @@ public: AMDGPUTargetInfo() {} void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; }; template class MipsTargetInfo final : public TargetInfo { public: MipsTargetInfo(); - RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + RelExpr getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const override; uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; uint32_t getDynRel(uint32_t Type) const override; void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; @@ -379,7 +389,8 @@ TlsGdToLeSkip = 2; } -RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { +RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { switch (Type) { default: return R_ABS; @@ -688,11 +699,25 @@ TlsGdToLeSkip = 2; } -RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { +bool X86_64TargetInfo::canRelaxGotPCRel(const SymbolBody &S, + const uint8_t *Loc) const { + if (Loc[-2] != 0x8b) + return false; + if (S.isUndefined() || S.isPreemptible()) + return false; + if (S.isGnuIFunc()) + return false; + return (S.getName() != "_DYNAMIC"); +} + +RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { switch (Type) { default: return R_ABS; case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + return canRelaxGotPCRel(S, Loc) ? R_X86_RELAX_GOT_PC : R_PC; case R_X86_64_PLT32: case R_X86_64_PC32: case R_X86_64_TLSLD: @@ -994,7 +1019,8 @@ } } -RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { +RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { return R_ABS; } @@ -1035,7 +1061,8 @@ return TocVA + 0x8000; } -RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { +RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { switch (Type) { default: return R_ABS; @@ -1203,8 +1230,8 @@ PltZeroSize = 32; } -RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type, - const SymbolBody &S) const { +RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { switch (Type) { default: return R_ABS; @@ -1558,7 +1585,8 @@ llvm_unreachable("not implemented"); } -RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { +RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { llvm_unreachable("not implemented"); } @@ -1576,8 +1604,8 @@ } template -RelExpr MipsTargetInfo::getRelExpr(uint32_t Type, - const SymbolBody &S) const { +RelExpr MipsTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S, + const uint8_t *Loc) const { switch (Type) { default: return R_ABS; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -411,6 +411,16 @@ return 0; } +template +static bool handleRelaxation(uint32_t Type, SymbolBody &Body, + InputSectionBase &C, uintX_t Offset, + uintX_t Addend, RelExpr Expr) { + if (Expr != R_X86_RELAX_GOT_PC) + return false; + C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); + return true; +} + // The reason we have to do this early scan is as follows // * To mmap the output file, we need to know the size // * For that, we need to know how many dynamic relocs we will have. @@ -469,13 +479,17 @@ if (Config->EMachine == EM_MIPS) Addend += findMipsPairedAddend(Buf, BufLoc, Body, &RI, E); - RelExpr Expr = Target->getRelExpr(Type, Body); + const uint8_t *Data = C.getSectionData().data(); + RelExpr Expr = Target->getRelExpr(Type, Body, Data + RI.r_offset); if (unsigned Processed = handleTlsRelocation(Type, Body, C, Offset, Addend, Expr)) { I += (Processed - 1); continue; } + if (handleRelaxation(Type, Body, C, Offset, Addend, Expr)) + continue; + if (Target->needsDynRelative(Type)) AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body, getAddend(RI)}); Index: test/ELF/Inputs/gotpc-relax-und-dso.s =================================================================== --- test/ELF/Inputs/gotpc-relax-und-dso.s +++ test/ELF/Inputs/gotpc-relax-und-dso.s @@ -0,0 +1,4 @@ +.globl dsofoo +.type dsofoo, @function +dsofoo: + nop Index: test/ELF/gotpc-relax-und-dso.s =================================================================== --- test/ELF/gotpc-relax-und-dso.s +++ test/ELF/gotpc-relax-und-dso.s @@ -0,0 +1,61 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/gotpc-relax-und-dso.s -o %tdso.o +# RUN: ld.lld -shared %tdso.o -o %t.so +# RUN: ld.lld -shared %t.o %t.so -o %tout +# RUN: llvm-readobj -r -s %tout | FileCheck --check-prefix=RELOC %s +# RUN: llvm-objdump -d %tout | FileCheck --check-prefix=DISASM %s + +# RELOC: Relocations [ +# RELOC-NEXT: Section ({{.*}}) .rela.dyn { +# RELOC-NEXT: 0x20A0 R_X86_64_GLOB_DAT und 0x0 +# RELOC-NEXT: 0x20A8 R_X86_64_GLOB_DAT dsofoo 0x0 +# RELOC-NEXT: 0x20B0 R_X86_64_GLOB_DAT foo 0x0 +# RELOC-NEXT: 0x20B8 R_X86_64_RELATIVE - 0x2000 +# RELOC-NEXT: } +# RELOC-NEXT: ] + +# 0x101e + 7 - 36 = 0x1001 +# 0x1025 + 7 - 43 = 0x1001 +# DISASM: Disassembly of section .text: +# DISASM-NEXT: foo: +# DISASM-NEXT: 1000: 90 nop +# DISASM: hid: +# DISASM-NEXT: 1001: 90 nop +# DISASM: _start: +# DISASM-NEXT: 1002: 48 8b 05 97 10 00 00 movq 4247(%rip), %rax +# DISASM-NEXT: 1009: 48 8b 05 90 10 00 00 movq 4240(%rip), %rax +# DISASM-NEXT: 1010: 48 8b 05 91 10 00 00 movq 4241(%rip), %rax +# DISASM-NEXT: 1017: 48 8b 05 8a 10 00 00 movq 4234(%rip), %rax +# DISASM-NEXT: 101e: 48 8d 05 dc ff ff ff leaq -36(%rip), %rax +# DISASM-NEXT: 1025: 48 8d 05 d5 ff ff ff leaq -43(%rip), %rax +# DISASM-NEXT: 102c: 48 8b 05 7d 10 00 00 movq 4221(%rip), %rax +# DISASM-NEXT: 1033: 48 8b 05 76 10 00 00 movq 4214(%rip), %rax +# DISASM-NEXT: 103a: 48 8b 05 77 10 00 00 movq 4215(%rip), %rax +# DISASM-NEXT: 1041: 48 8b 05 70 10 00 00 movq 4208(%rip), %rax + +.text +.globl foo +.type foo, @function +foo: + nop + +.globl hid +.hidden hid +.type hid, @function +hid: + nop + +.globl _start +.type _start, @function +_start: + movq und@GOTPCREL(%rip), %rax + movq und@GOTPCREL(%rip), %rax + movq dsofoo@GOTPCREL(%rip), %rax + movq dsofoo@GOTPCREL(%rip), %rax + movq hid@GOTPCREL(%rip), %rax + movq hid@GOTPCREL(%rip), %rax + movq foo@GOTPCREL(%rip), %rax + movq foo@GOTPCREL(%rip), %rax + movq _DYNAMIC@GOTPCREL(%rip), %rax + movq _DYNAMIC@GOTPCREL(%rip), %rax Index: test/ELF/gotpc-relax.s =================================================================== --- test/ELF/gotpc-relax.s +++ test/ELF/gotpc-relax.s @@ -0,0 +1,58 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: ld.lld %t.o -o %t1 +# RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=RELOC %s +# RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASM %s + +## There is no R_X86_64_GLOB_DAT relocations. +# RELOC: Relocations [ +# RELOC-NEXT: Section ({{.*}}) .rela.plt { +# RELOC-NOT: R_X86_64_GLOB_DAT + +# 0x11003 + 7 - 10 = 0x11000 +# 0x1100a + 7 - 17 = 0x11000 +# 0x11011 + 7 - 23 = 0x11001 +# 0x11018 + 7 - 30 = 0x11001 +# DISASM: Disassembly of section .text: +# DISASM-NEXT: foo: +# DISASM-NEXT: 11000: 90 nop +# DISASM: hid: +# DISASM-NEXT: 11001: 90 nop +# DISASM: ifunc: +# DISASM-NEXT: 11002: c3 retq +# DISASM: _start: +# DISASM-NEXT: 11003: 48 8d 05 f6 ff ff ff leaq -10(%rip), %rax +# DISASM-NEXT: 1100a: 48 8d 05 ef ff ff ff leaq -17(%rip), %rax +# DISASM-NEXT: 11011: 48 8d 05 e9 ff ff ff leaq -23(%rip), %rax +# DISASM-NEXT: 11018: 48 8d 05 e2 ff ff ff leaq -30(%rip), %rax +# DISASM-NEXT: 1101f: 48 8b 05 1a 00 00 00 movq 26(%rip), %rax +# DISASM-NEXT: 11026: 48 8b 05 13 00 00 00 movq 19(%rip), %rax + +.text +.globl foo +.type foo, @function +foo: + nop + +.globl hid +.hidden hid +.type hid, @function +hid: + nop + +.text +.type ifunc STT_GNU_IFUNC +.globl ifunc +.type ifunc, @function +ifunc: + ret + +.globl _start +.type _start, @function +_start: + movq foo@GOTPCREL(%rip), %rax + movq foo@GOTPCREL(%rip), %rax + movq hid@GOTPCREL(%rip), %rax + movq hid@GOTPCREL(%rip), %rax + movq ifunc@GOTPCREL(%rip), %rax + movq ifunc@GOTPCREL(%rip), %rax