[PATCH] D42748: [ELF] Don't create a .dynamic section when linking with -Bstatic

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 31 10:15:34 PST 2018


arichardson created this revision.
arichardson added reviewers: ruiu, rafael, ed.
Herald added subscribers: llvm-commits, krytarowski, sdardis, emaste.

Since https://reviews.llvm.org/rL295240 we add a dynamic symbol table to statically linked binaries.
This also causes a .dynamic section, the _DYNAMIC symtbol and a PT_DYNAMIC
header to be added to the output file. This causes problems for example
when trying to run such a binary on FreeBSD MIPS. All the operating system
tools and ELF loader treat this binary as a dynamically linked on instead
and therefore tries to load it with RTLD which will crash at runtime.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D42748

Files:
  ELF/Writer.cpp
  test/ELF/static-with-export-dynamic.s


Index: test/ELF/static-with-export-dynamic.s
===================================================================
--- test/ELF/static-with-export-dynamic.s
+++ test/ELF/static-with-export-dynamic.s
@@ -1,8 +1,22 @@
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-cloudabi %s -o %t.o
 // RUN: ld.lld --export-dynamic %t.o -o %t
-// RUN: llvm-readobj -dyn-symbols %t | FileCheck %s
+// BFD does not add a dynamic section when --export-dynamic is passed together
+// with -Bstatic so check that we do the same
+// RUN: ld.lld -Bstatic --export-dynamic %t.o -o %t-static
+// RUN: llvm-readobj -dyn-symbols -t -program-headers -s %t | FileCheck %s
+// RUN: llvm-readobj -dyn-symbols -t -program-headers -s %t-static | FileCheck %s -check-prefixes CHECK,STATIC
 // REQUIRES: x86
 
+
+// Ensure that the generated binary is still a static binary and doesn't have
+// the _DYNAMIC symbol or a .dynamic section
+// STATIC-LABEL: Sections [
+// STATIC-NOT:     Name: .interp
+// STATIC-NOT:     Name: .dynamic
+// STATIC:       ]
+// STATIC-LABEL: Symbols [
+// STATIC-NOT:     Name: _DYNAMIC
+// STATIC:       ]
 // Ensure that a dynamic symbol table is present when --export-dynamic
 // is passed in, even when creating statically linked executables.
 //
@@ -27,6 +41,9 @@
 // CHECK-NEXT:   }
 // CHECK-NEXT: ]
 
+// STATIC-LABEL: ProgramHeaders
+// STATIC-NOT: PT_DYNAMIC
+
 .global _start
 _start:
   ret
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -326,7 +326,8 @@
       Add(InX::HashTab);
     }
 
-    Add(InX::Dynamic);
+    if (!Config->Static)
+      Add(InX::Dynamic);
     Add(InX::DynStrTab);
     Add(InX::RelaDyn);
   }
@@ -1417,7 +1418,7 @@
   // It should be okay as no one seems to care about the type.
   // Even the author of gold doesn't remember why gold behaves that way.
   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
-  if (InX::DynSymTab)
+  if (InX::DynSymTab && !Config->Static)
     Symtab->addRegular("_DYNAMIC", STV_HIDDEN, STT_NOTYPE, 0 /*Value*/,
                        /*Size=*/0, STB_WEAK, InX::Dynamic,
                        /*File=*/nullptr);
@@ -1678,7 +1679,7 @@
     Ret.push_back(TlsHdr);
 
   // Add an entry for .dynamic.
-  if (InX::DynSymTab)
+  if (InX::DynSymTab && !Config->Static)
     AddHdr(PT_DYNAMIC, InX::Dynamic->getParent()->getPhdrFlags())
         ->add(InX::Dynamic->getParent());
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42748.132206.patch
Type: text/x-patch
Size: 2442 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180131/fc570fed/attachment.bin>


More information about the llvm-commits mailing list