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()); 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