Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -713,6 +713,10 @@ } else { // We don't know anything about the finaly symbol. Just ask the dynamic // linker to handle the relocation for us. + if (!Target->isPICRel(Type)) + error(getLocation(C, Offset) + ": relocation " + getRelName(Type) + + " cannot be used against shared object; recompile with -fPIC."); + AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend}); // MIPS ABI turns using of GOT and dynamic relocations inside out. // While regular ABI uses dynamic relocations to fill up GOT entries Index: ELF/Target.h =================================================================== --- ELF/Target.h +++ ELF/Target.h @@ -26,6 +26,7 @@ virtual bool isTlsInitialExecRel(uint32_t Type) const; virtual bool isTlsLocalDynamicRel(uint32_t Type) const; virtual bool isTlsGlobalDynamicRel(uint32_t Type) const; + virtual bool isPICRel(uint32_t Type) const { return true; } virtual uint32_t getDynRel(uint32_t Type) const { return Type; } virtual void writeGotPltHeader(uint8_t *Buf) const {} virtual void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {}; Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -75,11 +75,6 @@ error("improper alignment for relocation " + getRelName(Type)); } -static void errorDynRel(uint32_t Type) { - error("relocation " + getRelName(Type) + - " cannot be used against shared object; recompile with -fPIC."); -} - namespace { class X86TargetInfo final : public TargetInfo { public: @@ -109,7 +104,7 @@ public: X86_64TargetInfo(); RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; - uint32_t getDynRel(uint32_t Type) const override; + bool isPICRel(uint32_t Type) const override; bool isTlsLocalDynamicRel(uint32_t Type) const override; bool isTlsGlobalDynamicRel(uint32_t Type) const override; bool isTlsInitialExecRel(uint32_t Type) const override; @@ -153,6 +148,7 @@ public: AArch64TargetInfo(); RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + bool isPICRel(uint32_t Type) const override; uint32_t getDynRel(uint32_t Type) const override; bool isTlsInitialExecRel(uint32_t Type) const override; void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override; @@ -179,6 +175,7 @@ public: ARMTargetInfo(); RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; + bool isPICRel(uint32_t Type) const override; uint32_t getDynRel(uint32_t Type) const override; uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; bool isTlsLocalDynamicRel(uint32_t Type) const override; @@ -198,6 +195,7 @@ MipsTargetInfo(); RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; + bool isPICRel(uint32_t Type) const override; uint32_t getDynRel(uint32_t Type) const override; bool isTlsLocalDynamicRel(uint32_t Type) const override; bool isTlsGlobalDynamicRel(uint32_t Type) const override; @@ -637,10 +635,8 @@ } template -uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const { - if (Type == R_X86_64_PC32 || Type == R_X86_64_32) - errorDynRel(Type); - return Type; +bool X86_64TargetInfo::isPICRel(uint32_t Type) const { + return Type != R_X86_64_PC32 && Type != R_X86_64_32; } template @@ -1246,12 +1242,12 @@ Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC; } +bool AArch64TargetInfo::isPICRel(uint32_t Type) const { + return Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64; +} + uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const { - if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64) - return Type; - // Keep it going with a dummy value so that we can find more reloc errors. - errorDynRel(Type); - return R_AARCH64_ABS32; + return isPICRel(Type) ? Type : R_AARCH64_ABS32; } void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &) const { @@ -1606,13 +1602,17 @@ } } +bool ARMTargetInfo::isPICRel(uint32_t Type) const { + return (Type == R_ARM_TARGET1 && !Config->Target1Rel) || + (Type == R_ARM_ABS32); +} + uint32_t ARMTargetInfo::getDynRel(uint32_t Type) const { if (Type == R_ARM_TARGET1 && !Config->Target1Rel) return R_ARM_ABS32; if (Type == R_ARM_ABS32) return Type; // Keep it going with a dummy value so that we can find more reloc errors. - errorDynRel(Type); return R_ARM_ABS32; } @@ -1976,13 +1976,13 @@ } } +template bool MipsTargetInfo::isPICRel(uint32_t Type) const { + return (Type == R_MIPS_32 || Type == R_MIPS_64); +} + template uint32_t MipsTargetInfo::getDynRel(uint32_t Type) const { - if (Type == R_MIPS_32 || Type == R_MIPS_64) - return RelativeRel; - // Keep it going with a dummy value so that we can find more reloc errors. - errorDynRel(Type); - return R_MIPS_32; + return isPICRel(Type) ? RelativeRel : R_MIPS_32; } template Index: test/ELF/aarch64-fpic-abs16.s =================================================================== --- test/ELF/aarch64-fpic-abs16.s +++ test/ELF/aarch64-fpic-abs16.s @@ -1,7 +1,7 @@ // REQUIRES: aarch64 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o // RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s -// CHECK: relocation R_AARCH64_ABS16 cannot be used against shared object; recompile with -fPIC. +// CHECK: {{.*}}:(.data+0x0): relocation R_AARCH64_ABS16 cannot be used against shared object; recompile with -fPIC. .data .hword foo Index: test/ELF/aarch64-fpic-prel16.s =================================================================== --- test/ELF/aarch64-fpic-prel16.s +++ test/ELF/aarch64-fpic-prel16.s @@ -1,7 +1,7 @@ // REQUIRES: aarch64 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o // RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s -// CHECK: relocation R_AARCH64_PREL16 cannot be used against shared object; recompile with -fPIC. +// CHECK: {{.*}}:(.data+0x0): relocation R_AARCH64_PREL16 cannot be used against shared object; recompile with -fPIC. .data .hword foo - . Index: test/ELF/aarch64-fpic-prel32.s =================================================================== --- test/ELF/aarch64-fpic-prel32.s +++ test/ELF/aarch64-fpic-prel32.s @@ -1,7 +1,7 @@ // REQUIRES: aarch64 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o // RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s -// CHECK: relocation R_AARCH64_PREL32 cannot be used against shared object; recompile with -fPIC. +// CHECK: {{.*}}:(.data+0x0): relocation R_AARCH64_PREL32 cannot be used against shared object; recompile with -fPIC. .data .word foo - . Index: test/ELF/aarch64-fpic-prel64.s =================================================================== --- test/ELF/aarch64-fpic-prel64.s +++ test/ELF/aarch64-fpic-prel64.s @@ -1,7 +1,7 @@ // REQUIRES: aarch64 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o // RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s -// CHECK: relocation R_AARCH64_PREL64 cannot be used against shared object; recompile with -fPIC. +// CHECK: {{.*}}:(.data+0x0): relocation R_AARCH64_PREL64 cannot be used against shared object; recompile with -fPIC. .data .xword foo - . Index: test/ELF/x86-64-dyn-rel-error.s =================================================================== --- test/ELF/x86-64-dyn-rel-error.s +++ test/ELF/x86-64-dyn-rel-error.s @@ -9,4 +9,4 @@ .data .long bar -// CHECK: R_X86_64_32 cannot be used against shared object; recompile with -fPIC. +// CHECK: {{.*}}:(.data+0x0): relocation R_X86_64_32 cannot be used against shared object; recompile with -fPIC. Index: test/ELF/x86-64-dyn-rel-error2.s =================================================================== --- test/ELF/x86-64-dyn-rel-error2.s +++ test/ELF/x86-64-dyn-rel-error2.s @@ -9,4 +9,4 @@ .data .long bar - . -// CHECK: R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC. +// CHECK: {{.*}}:(.data+0x0): relocation R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC. Index: test/ELF/x86-64-reloc-32-fpic.s =================================================================== --- test/ELF/x86-64-reloc-32-fpic.s +++ test/ELF/x86-64-reloc-32-fpic.s @@ -1,7 +1,7 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o # RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s -# CHECK: relocation R_X86_64_32 cannot be used against shared object; recompile with -fPIC. +# CHECK: {{.*}}:(.data+0x0): relocation R_X86_64_32 cannot be used against shared object; recompile with -fPIC. .data .long _shared Index: test/ELF/x86-64-reloc-pc32-fpic.s =================================================================== --- test/ELF/x86-64-reloc-pc32-fpic.s +++ test/ELF/x86-64-reloc-pc32-fpic.s @@ -1,7 +1,7 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o # RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s -# CHECK: relocation R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC. +# CHECK: {{.*}}:(.data+0x1): relocation R_X86_64_PC32 cannot be used against shared object; recompile with -fPIC. .data call _shared