Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -2586,6 +2586,8 @@ // The sentinel has to be removed if there are no other .ARM.exidx entries. bool ARMExidxSentinelSection::empty() const { OutputSection *OS = getParent(); + if (!OS) + return true; for (auto *B : OS->SectionCommands) if (auto *ISD = dyn_cast(B)) for (auto *S : ISD->Sections) Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1309,7 +1309,7 @@ if (!SS) return; OutputSection *OS = SS->getParent(); - if (!SS->empty() || !OS) + if (!OS || !SS->empty()) continue; std::vector::iterator Empty = OS->SectionCommands.end(); Index: test/ELF/arm-exidx-discard.s =================================================================== --- test/ELF/arm-exidx-discard.s +++ test/ELF/arm-exidx-discard.s @@ -0,0 +1,14 @@ +// RUN: llvm-mc -filetype=obj -triple arm-gnu-linux-eabi -mcpu cortex-a7 -arm-add-build-attributes %s -o %t.o +// RUN: echo "ENTRY(__entrypoint) SECTIONS { . = 0x10000; .text : { *(.text .text.*) } /DISCARD/ : { *(.ARM.exidx*) *(.gnu.linkonce.armexidx.*) } }" > %t.script +// RUN: ld.lld -T %t.script %t.o -o %t.elf 2>&1 +// RUN: llvm-readobj -sections %t.elf | FileCheck %s +// REQUIRES: arm + +.globl __entrypoint +__entrypoint: + bx lr + +// Check that .ARM.exidx/.gnu.linkonce.armexidx +// are correctly removed if they were added. +// CHECK-NOT: .ARM.exidx +// CHECK-NOT: .gnu.linkonce.armexidx.