[PATCH] D105164: [ELF] -pie: produce dynamic relocations for absolute relocations referencing undef weak

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 29 17:36:46 PDT 2021


MaskRay created this revision.
MaskRay added a reviewer: peter.smith.
Herald added subscribers: pengfei, arichardson, emaste.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

See the comment for my understanding of -no-pie and -shared expectation.
-no-pie has freedom on choices. We choose dynamic relocations to be consistent
with the handling of GOT-generating relocations.

Note: GNU ld has arch-varying behaviors and its x86 -pie has a very
complex rule:
if there is at least one GOT-generating or PLT-generating relocation and
-z dynamic-undefined-weak (enabled by default) is in effect, generate a
dynamic relocation.

We don't emulate its rule.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105164

Files:
  lld/ELF/Relocations.cpp
  lld/test/ELF/weak-undef-rw.s


Index: lld/test/ELF/weak-undef-rw.s
===================================================================
--- lld/test/ELF/weak-undef-rw.s
+++ lld/test/ELF/weak-undef-rw.s
@@ -1,13 +1,24 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: ld.lld %t.o -o %t --export-dynamic
-# RUN: llvm-readelf -r %t | FileCheck %s
+# RUN: llvm-readelf -r %t | FileCheck %s --check-prefix=NOPIC
+# RUN: ld.lld %t.o -o %t.pie -pie
+# RUN: llvm-readobj -r %t.pie | FileCheck %s --check-prefix=PIC
+# RUN: ld.lld %t.o -o %t.so -shared
+# RUN: llvm-readobj -r %t.so | FileCheck %s --check-prefix=PIC
 
 ## gABI leaves the behavior of weak undefined references implementation defined.
-## We choose to resolve it statically and not create a dynamic relocation for
-## implementation simplicity. This also matches ld.bfd and gold.
+## We choose to resolve them statically for -no-pie and produce dynamic relocations
+## for -pie and -shared.
+##
+## Note: Some ports of GNU ld support -z nodynamic-undefined-weak that we don't
+## implement.
 
-# CHECK: no relocations
+# NOPIC: no relocations
+
+# PIC:      .rela.dyn {
+# PIC-NEXT:   R_X86_64_64 foobar 0x0
+# PIC-NEXT: }
 
         .global _start
 _start:
Index: lld/ELF/Relocations.cpp
===================================================================
--- lld/ELF/Relocations.cpp
+++ lld/ELF/Relocations.cpp
@@ -1147,11 +1147,18 @@
   // relocation will be created, pass the control to relocateAlloc() or
   // relocateNonAlloc() to resolve it.
   //
-  // The behavior of an undefined weak reference is implementation defined. If
-  // the relocation is to a weak undef, and we are producing an executable, let
-  // relocate{,Non}Alloc() resolve it.
+  // The behavior of an undefined weak reference is implementation defined. For
+  // non-link-time constant, we resolve relocations statically (let
+  // relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic
+  // relocations for -pie and -shared.
+  //
+  // The general expectation of -no-pie static linking is that there is no
+  // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for
+  // -shared matches the spirit of its -z undefs default. -pie has freedom on
+  // choices, and we choose dynamic relocations to be consistent with the
+  // handling of GOT-generating relocations.
   if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
-      (!config->shared && sym.isUndefWeak())) {
+      (!config->isPic && sym.isUndefWeak())) {
     sec.relocations.push_back({expr, type, offset, addend, &sym});
     return;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105164.355411.patch
Type: text/x-patch
Size: 2603 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210630/751f9797/attachment.bin>


More information about the llvm-commits mailing list