[PATCH] D41551: [ELF] - Allow relocation to a weak undefined symbol when -z notext is given.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 25 14:33:44 PST 2017


A rebased version LGTM.

Thanks,
Rafael

George Rimar via Phabricator <reviews at reviews.llvm.org> writes:

> grimar updated this revision to Diff 128095.
> grimar added a comment.
>
> - Updated comment.
>
>
> https://reviews.llvm.org/D41551
>
> Files:
>   ELF/Relocations.cpp
>   test/ELF/znotext-weak-undef.s
>
>
> Index: test/ELF/znotext-weak-undef.s
> ===================================================================
> --- test/ELF/znotext-weak-undef.s
> +++ test/ELF/znotext-weak-undef.s
> @@ -0,0 +1,14 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
> +# RUN: ld.lld -z notext -shared %t.o -o %t
> +# RUN: llvm-readobj -r %t | FileCheck %s
> +
> +# CHECK:      Relocations [
> +# CHECK-NEXT: ]
> +
> +.text
> +.global foo
> +.weak foo
> +
> +_start:
> +mov $foo,%eax
> Index: ELF/Relocations.cpp
> ===================================================================
> --- ELF/Relocations.cpp
> +++ ELF/Relocations.cpp
> @@ -589,33 +589,33 @@
>    if (S.Flags & SHF_WRITE)
>      return Expr;
>  
> -  // Or, if we are allowed to create dynamic relocations against
> -  // read-only sections (i.e. when "-z notext" is given),
> -  // we can create a dynamic relocation as we want, too.
> -  if (!Config->ZText) {
> -    // We use PLT for relocations that may overflow in runtime,
> -    // see comment for getPltExpr().
> -    if (Sym.isFunc() && !Target->isPicRel(Type))
> -      return getPltExpr(Sym, Expr, IsConstant);
> -    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;
>    }
>  
> +  // If we are allowed to create dynamic relocations against
> +  // read-only sections (i.e. when "-z notext" is given),
> +  // we can create a dynamic relocation as we want.
> +  if (!Config->ZText) {
> +    // We use PLT for relocations that may overflow in runtime,
> +    // see comment for getPltExpr().
> +    if (Sym.isFunc() && !Target->isPicRel(Type))
> +      return getPltExpr(Sym, Expr, 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.
> +
>    // 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))) {
>
>
> Index: test/ELF/znotext-weak-undef.s
> ===================================================================
> --- test/ELF/znotext-weak-undef.s
> +++ test/ELF/znotext-weak-undef.s
> @@ -0,0 +1,14 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
> +# RUN: ld.lld -z notext -shared %t.o -o %t
> +# RUN: llvm-readobj -r %t | FileCheck %s
> +
> +# CHECK:      Relocations [
> +# CHECK-NEXT: ]
> +
> +.text
> +.global foo
> +.weak foo
> +
> +_start:
> +mov $foo,%eax
> Index: ELF/Relocations.cpp
> ===================================================================
> --- ELF/Relocations.cpp
> +++ ELF/Relocations.cpp
> @@ -589,33 +589,33 @@
>    if (S.Flags & SHF_WRITE)
>      return Expr;
>  
> -  // Or, if we are allowed to create dynamic relocations against
> -  // read-only sections (i.e. when "-z notext" is given),
> -  // we can create a dynamic relocation as we want, too.
> -  if (!Config->ZText) {
> -    // We use PLT for relocations that may overflow in runtime,
> -    // see comment for getPltExpr().
> -    if (Sym.isFunc() && !Target->isPicRel(Type))
> -      return getPltExpr(Sym, Expr, IsConstant);
> -    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;
>    }
>  
> +  // If we are allowed to create dynamic relocations against
> +  // read-only sections (i.e. when "-z notext" is given),
> +  // we can create a dynamic relocation as we want.
> +  if (!Config->ZText) {
> +    // We use PLT for relocations that may overflow in runtime,
> +    // see comment for getPltExpr().
> +    if (Sym.isFunc() && !Target->isPicRel(Type))
> +      return getPltExpr(Sym, Expr, 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.
> +
>    // 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))) {


More information about the llvm-commits mailing list