Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -144,6 +144,7 @@ void createSections(OutputSectionFactory &Factory); std::vector> createPhdrs(); + bool discardInterpSection(); ArrayRef getFiller(StringRef Name); bool shouldKeep(InputSectionBase *S); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -486,6 +486,15 @@ return Ret; } +template bool LinkerScript::discardInterpSection() { + // Discard .interp section in case we have PHDRS specification + // and PT_INTERP isn't listed. + return !Opt.PhdrsCommands.empty() && + llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) { + return Cmd.Type == PT_INTERP; + }) == Opt.PhdrsCommands.end(); +} + template ArrayRef LinkerScript::getFiller(StringRef Name) { for (const std::unique_ptr &Base : Opt.Commands) Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -105,7 +105,8 @@ template static bool needsInterpSection() { return !Symtab::X->getSharedFiles().empty() && - !Config->DynamicLinker.empty(); + !Config->DynamicLinker.empty() && + !Script::X->discardInterpSection(); } template void elf::writeResult() { Index: test/ELF/linkerscript/linkerscript-discard-interp.s =================================================================== --- test/ELF/linkerscript/linkerscript-discard-interp.s +++ test/ELF/linkerscript/linkerscript-discard-interp.s @@ -0,0 +1,12 @@ +// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o +// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/../Inputs/shared.s -o %t2.o +// RUN: ld.lld -shared %t2.o -o %t2.so +// RUN: echo "PHDRS { text PT_LOAD FILEHDR PHDRS; } \ +// RUN: SECTIONS { .text : { *(.text) } : text }" >> %t.script +// RUN: ld.lld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -rpath foo -rpath bar --script %t.script --export-dynamic %t.o %t2.so -o %t +// RUN: llvm-readobj -s %t | FileCheck %s + +// CHECK-NOT: Name: .interp + +.global _start +_start: