Index: lld/trunk/ELF/Relocations.cpp =================================================================== --- lld/trunk/ELF/Relocations.cpp +++ lld/trunk/ELF/Relocations.cpp @@ -711,7 +711,11 @@ } 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 " + toString(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 // MIPS ABI requires dynamic linker to fills up GOT entries using Index: lld/trunk/ELF/Target.h =================================================================== --- lld/trunk/ELF/Target.h +++ lld/trunk/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: lld/trunk/ELF/Target.cpp =================================================================== --- lld/trunk/ELF/Target.cpp +++ lld/trunk/ELF/Target.cpp @@ -75,11 +75,6 @@ error("improper alignment for relocation " + toString(Type)); } -static void errorDynRel(uint32_t Type) { - error("relocation " + toString(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,7 +148,7 @@ public: AArch64TargetInfo(); 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 isTlsInitialExecRel(uint32_t Type) const override; void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override; void writePltHeader(uint8_t *Buf) const override; @@ -179,6 +174,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 +194,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; @@ -640,10 +637,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 @@ -1249,12 +1244,8 @@ Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC; } -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; +bool AArch64TargetInfo::isPicRel(uint32_t Type) const { + return Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64; } void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &) const { @@ -1609,13 +1600,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; } @@ -1979,13 +1974,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 RelativeRel; } template Index: lld/trunk/test/ELF/aarch64-fpic-abs16.s =================================================================== --- lld/trunk/test/ELF/aarch64-fpic-abs16.s +++ lld/trunk/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: lld/trunk/test/ELF/aarch64-fpic-prel16.s =================================================================== --- lld/trunk/test/ELF/aarch64-fpic-prel16.s +++ lld/trunk/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: lld/trunk/test/ELF/aarch64-fpic-prel32.s =================================================================== --- lld/trunk/test/ELF/aarch64-fpic-prel32.s +++ lld/trunk/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: lld/trunk/test/ELF/aarch64-fpic-prel64.s =================================================================== --- lld/trunk/test/ELF/aarch64-fpic-prel64.s +++ lld/trunk/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: lld/trunk/test/ELF/x86-64-dyn-rel-error.s =================================================================== --- lld/trunk/test/ELF/x86-64-dyn-rel-error.s +++ lld/trunk/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: lld/trunk/test/ELF/x86-64-dyn-rel-error2.s =================================================================== --- lld/trunk/test/ELF/x86-64-dyn-rel-error2.s +++ lld/trunk/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: lld/trunk/test/ELF/x86-64-reloc-32-fpic.s =================================================================== --- lld/trunk/test/ELF/x86-64-reloc-32-fpic.s +++ lld/trunk/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: lld/trunk/test/ELF/x86-64-reloc-pc32-fpic.s =================================================================== --- lld/trunk/test/ELF/x86-64-reloc-pc32-fpic.s +++ lld/trunk/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