summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Rimar <grimar@accesssoftek.com>2017-12-27 07:29:55 +0000
committerGeorge Rimar <grimar@accesssoftek.com>2017-12-27 07:29:55 +0000
commita0ab8d7a5890f0c4c3dc7b1c919fe7d65251ab31 (patch)
treedaf20b28e9bb7da97ad58f3a5e71827980261574
parentedf3c8292b3694f1fb6dd4ef88d872aa22b2add9 (diff)
downloadbcm5719-llvm-a0ab8d7a5890f0c4c3dc7b1c919fe7d65251ab31.tar.gz
bcm5719-llvm-a0ab8d7a5890f0c4c3dc7b1c919fe7d65251ab31.zip
[ELF] - Allow relocation to a weak undefined symbol when -z notext is given.
Previously we failed to resolve them when produced executables: "relocation R_X86_64_32 cannot be used against shared object; recompile with -fPIC" Patch fixes it so that we resolve them to 0 for executables. And for -shared case we still should produce the relocation. This finishes fixing PR35720. DIfferential revision: https://reviews.llvm.org/D41551 llvm-svn: 321473
-rw-r--r--lld/ELF/Relocations.cpp26
-rw-r--r--lld/test/ELF/znotext-weak-undef.s16
2 files changed, 29 insertions, 13 deletions
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index e9efb3c7779..87798e8e748 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -585,6 +585,19 @@ template <class ELFT>
static RelExpr adjustExpr(Symbol &Sym, RelExpr Expr, RelType Type,
InputSectionBase &S, uint64_t RelOff,
bool &IsConstant) {
+ // If a relocation can be applied at link-time, we don't need to
+ // create a dynamic relocation in the first place.
+ if (IsConstant)
+ return Expr;
+
+ // If the relocation is to a weak undef, and we are producing
+ // executable, give up on it and produce a non preemptible 0.
+ if (!Config->Shared && Sym.isUndefWeak()) {
+ Sym.IsPreemptible = false;
+ IsConstant = true;
+ return Expr;
+ }
+
// If a section writable or if we are allowed to create dynamic relocations
// against read-only sections (i.e. when "-z notext" is given), we can create
// any dynamic relocation the dynamic linker knows how to handle.
@@ -596,22 +609,9 @@ static RelExpr adjustExpr(Symbol &Sym, RelExpr Expr, RelType Type,
return Expr;
}
- // If a relocation can be applied at link-time, we don't need to
- // create a dynamic relocation in the first place.
- if (IsConstant)
- return Expr;
-
// If we got here we know that this relocation would require the dynamic
// linker to write a value to read only memory.
- // If the relocation is to a weak undef, give up on it and produce a
- // non preemptible 0.
- if (Sym.isUndefWeak()) {
- Sym.IsPreemptible = false;
- IsConstant = true;
- return Expr;
- }
-
// We can hack around it if we are producing an executable and
// the refered symbol can be preemepted to refer to the executable.
if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
diff --git a/lld/test/ELF/znotext-weak-undef.s b/lld/test/ELF/znotext-weak-undef.s
new file mode 100644
index 00000000000..d606d872bc4
--- /dev/null
+++ b/lld/test/ELF/znotext-weak-undef.s
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: not ld.lld -z notext -shared %t.o -o %t 2>&1 | FileCheck %s
+# CHECK: relocation R_X86_64_32 cannot be used against shared object; recompile with -fPIC
+
+# RUN: ld.lld -z notext %t.o -o %t
+# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=EXE
+# EXE: Relocations [
+# EXE-NEXT: ]
+
+.text
+.global foo
+.weak foo
+
+_start:
+mov $foo,%eax
OpenPOWER on IntegriCloud