diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index dd9ef6a7f9a9..7195d8f6c414 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -486,6 +486,15 @@ std::vector> LinkerScript::createPhdrs() { return Ret; } +template bool LinkerScript::ignoreInterpSection() { + // Ignore .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) diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 53a6db5632a7..d33c12e41eef 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -144,6 +144,7 @@ public: void createSections(OutputSectionFactory &Factory); std::vector> createPhdrs(); + bool ignoreInterpSection(); ArrayRef getFiller(StringRef Name); bool shouldKeep(InputSectionBase *S); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 108ca54afea2..5a395599ed07 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -105,7 +105,8 @@ template void elf::reportDiscarded(InputSectionBase *IS) { template static bool needsInterpSection() { return !Symtab::X->getSharedFiles().empty() && - !Config->DynamicLinker.empty(); + !Config->DynamicLinker.empty() && + !Script::X->ignoreInterpSection(); } template void elf::writeResult() { diff --git a/lld/test/ELF/linkerscript/linkerscript-discard-interp.s b/lld/test/ELF/linkerscript/linkerscript-discard-interp.s new file mode 100644 index 000000000000..5f2d82ea4ab2 --- /dev/null +++ b/lld/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: